<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<script>
    (function () {
        if ('') {
            if (prompt('请输入文章密码') !== '') {
                alert('密码错误！');
                if (history.length === 1) {
                    location.replace("https://google.com"); // 这里替换成你的首页
                } else {
                    history.back();
                }
            }
        }
    })();
</script>








<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta property="og:type" content="website">
<meta property="og:title" content="苏生不惑的博客">
<meta property="og:url" content="http://yoursite.com/page/26/index.html">
<meta property="og:site_name" content="苏生不惑的博客">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="苏生不惑的博客">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/page/26/">



<meta name="referrer" content="never"> ​​​​


  <title>苏生不惑的博客</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">苏生不惑的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/16/laravel-记录/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/10/16/laravel-记录/" itemprop="url">laravel 记录</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-10-16T19:45:15+08:00">
                2019-10-16
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  17.1k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  85 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="Laravel-网站不会被嵌入到其他站点中"><a href="#Laravel-网站不会被嵌入到其他站点中" class="headerlink" title="Laravel 网站不会被嵌入到其他站点中"></a>Laravel 网站不会被嵌入到其他站点中</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">HTTP 响应头部中，有一个字段，叫做 X-Frame-Options，该字段可以用来指示是否允许自己的网站被嵌入到其他网站的 &lt;iframe&gt; 或者 &lt;object&gt; 标签中。该头部有三个值</span><br><span class="line"></span><br><span class="line">DENY - 始终不允许嵌入，即使是同一个域名</span><br><span class="line">SAMEORIGIN - 只能在相同域名中嵌入</span><br><span class="line">ALLOW-FROM uri - 设置允许的域</span><br><span class="line">通常，可以在 HTTP 代理中进行配置，比如 nginx</span><br><span class="line"></span><br><span class="line">add_header X-Frame-Options SAMEORIGIN;</span><br><span class="line">Laravel 自带了用来「只允许同域名嵌入」的中间件，我们只需要在 /app/Http/Kernel.php 中添加即可</span><br><span class="line"></span><br><span class="line"><span class="comment">// /app/Http/Kernel.php</span></span><br><span class="line">protected $middleware = [</span><br><span class="line">    \Illuminate\Http\Middleware\FrameGuard::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">];</span><br><span class="line">该中间件的实现如下</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace Illuminate\Http\Middleware;</span><br><span class="line"></span><br><span class="line">use Closure;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">FrameGuard</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Handle the given request and get the response.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @param  \Illuminate\Http\Request  $request</span></span><br><span class="line"><span class="comment">     * @param  \Closure  $next</span></span><br><span class="line"><span class="comment">     * @return \Symfony\Component\HttpFoundation\Response</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">$request, Closure $next</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $response = $next($request);</span><br><span class="line"></span><br><span class="line">        $response-&gt;headers-&gt;set(<span class="string">'X-Frame-Options'</span>, <span class="string">'SAMEORIGIN'</span>, <span class="literal">false</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $response;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/35201</span></span><br><span class="line">https:<span class="comment">//securityheaders.com/</span></span><br><span class="line"></span><br><span class="line">可以直接扫描特定 URL 是否包含一些安全头信息。类似的头还有 X-Content-Type-Options、 Referrer-Policy、Feature-Policy 等。</span><br></pre></td></tr></table></figure>
<h3 id="Uncaught-ReflectionException-Class-request-does-not-exist"><a href="#Uncaught-ReflectionException-Class-request-does-not-exist" class="headerlink" title="Uncaught ReflectionException: Class request does not exist"></a>Uncaught ReflectionException: Class request does not exist</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">PHP Fatal error:  Uncaught ReflectionException: Class request does not exist <span class="keyword">in</span> /home/vagrant/learnku/vendor/laravel/framework/src/Illuminate/Container/Container.php:<span class="number">790</span></span><br><span class="line">Stack trace:</span><br><span class="line">在 App\Exceptions\Handler::report() 方法里，使用：</span><br><span class="line"></span><br><span class="line">dd($exception);</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-下-TNTSearch-jieba-PHP-实现中文全文搜索"><a href="#Laravel-下-TNTSearch-jieba-PHP-实现中文全文搜索" class="headerlink" title="Laravel 下 TNTSearch+jieba-PHP 实现中文全文搜索"></a>Laravel 下 TNTSearch+jieba-PHP 实现中文全文搜索</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line">TNTSearch+jieba-php 这套组合可以在不依赖第三方的情况下实现中文全文搜索；</span><br><span class="line">composer <span class="built_in">require</span> vanry/laravel-scout-tntsearch</span><br><span class="line"><span class="string">'providers'</span> =&gt; [</span><br><span class="line">    ...</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * TNTSearch 全文搜索</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    Laravel\Scout\ScoutServiceProvider::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">    Vanry\Scout\TNTSearchScoutServiceProvider::class,</span><br><span class="line">],</span><br><span class="line">composer <span class="built_in">require</span> fukuball/jieba-php</span><br><span class="line"></span><br><span class="line">php artisan vendor:publish --provider=<span class="string">"Laravel\Scout\ScoutServiceProvider"</span></span><br><span class="line"></span><br><span class="line">配置项 config/scout.php 中增加 tntsearch</span><br><span class="line"></span><br><span class="line"><span class="string">'tntsearch'</span> =&gt; [</span><br><span class="line">    <span class="string">'storage'</span> =&gt; storage_path(<span class="string">'indexes'</span>), <span class="comment">//必须有可写权限</span></span><br><span class="line">    <span class="string">'fuzziness'</span> =&gt; env(<span class="string">'TNTSEARCH_FUZZINESS'</span>, <span class="literal">false</span>),</span><br><span class="line">    <span class="string">'searchBoolean'</span> =&gt; env(<span class="string">'TNTSEARCH_BOOLEAN'</span>, <span class="literal">false</span>),</span><br><span class="line">    <span class="string">'asYouType'</span> =&gt; <span class="literal">false</span>,</span><br><span class="line"></span><br><span class="line">    <span class="string">'fuzzy'</span> =&gt; [</span><br><span class="line">        <span class="string">'prefix_length'</span> =&gt; <span class="number">2</span>,</span><br><span class="line">        <span class="string">'max_expansions'</span> =&gt; <span class="number">50</span>,</span><br><span class="line">        <span class="string">'distance'</span> =&gt; <span class="number">2</span>,</span><br><span class="line">    ],</span><br><span class="line"></span><br><span class="line">    <span class="string">'tokenizer'</span> =&gt; [</span><br><span class="line">        <span class="string">'driver'</span> =&gt; env(<span class="string">'TNTSEARCH_TOKENIZER'</span>, <span class="string">'default'</span>),</span><br><span class="line"></span><br><span class="line">        <span class="string">'jieba'</span> =&gt; [</span><br><span class="line">            <span class="string">'dict'</span> =&gt; <span class="string">'small'</span>,</span><br><span class="line">            <span class="comment">//'user_dict' =&gt; resource_path('dicts/mydict.txt'), //自定义词典路径</span></span><br><span class="line">        ],</span><br><span class="line"></span><br><span class="line">        <span class="string">'analysis'</span> =&gt; [</span><br><span class="line">            <span class="string">'result_type'</span> =&gt; <span class="number">2</span>,</span><br><span class="line">            <span class="string">'unit_word'</span> =&gt; <span class="literal">true</span>,</span><br><span class="line">            <span class="string">'differ_max'</span> =&gt; <span class="literal">true</span>,</span><br><span class="line">        ],</span><br><span class="line"></span><br><span class="line">        <span class="string">'scws'</span> =&gt; [</span><br><span class="line">            <span class="string">'charset'</span> =&gt; <span class="string">'utf-8'</span>,</span><br><span class="line">            <span class="string">'dict'</span> =&gt; <span class="string">'/usr/local/scws/etc/dict.utf8.xdb'</span>,</span><br><span class="line">            <span class="string">'rule'</span> =&gt; <span class="string">'/usr/local/scws/etc/rules.utf8.ini'</span>,</span><br><span class="line">            <span class="string">'multi'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">            <span class="string">'ignore'</span> =&gt; <span class="literal">true</span>,</span><br><span class="line">            <span class="string">'duality'</span> =&gt; <span class="literal">false</span>,</span><br><span class="line">        ],</span><br><span class="line">    ],</span><br><span class="line"></span><br><span class="line">    <span class="string">'stopwords'</span> =&gt; [</span><br><span class="line">        <span class="string">'的'</span>,</span><br><span class="line">        <span class="string">'了'</span>,</span><br><span class="line">        <span class="string">'而是'</span>,</span><br><span class="line">    ],</span><br><span class="line">],</span><br><span class="line"> env 增加配置项</span><br><span class="line"></span><br><span class="line">SCOUT_DRIVER=tntsearch</span><br><span class="line">TNTSEARCH_TOKENIZER=jieba</span><br><span class="line">namespace App\Models;</span><br><span class="line"></span><br><span class="line">use Illuminate\Database\Eloquent\Model;</span><br><span class="line">use Laravel\Scout\Searchable;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Article</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    use Searchable;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 索引的字段</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return array</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">toSearchableArray</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;only(<span class="string">'id'</span>, <span class="string">'title'</span>, <span class="string">'content'</span>);</span><br><span class="line"></span><br><span class="line">       <span class="comment">// return $this-&gt;toArray();</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">生成索引：</span><br><span class="line"></span><br><span class="line">php artisan scout:<span class="keyword">import</span> <span class="string">"App\Model\Article"</span></span><br><span class="line">https:<span class="comment">//learnku.com/articles/27617#replies</span></span><br></pre></td></tr></table></figure>
<h3 id="二分查找"><a href="#二分查找" class="headerlink" title="二分查找"></a>二分查找</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line">原理实现：有序集合从中间分为前后<span class="number">2</span>部分，当要查当数值等于中间值，直接返回；当要查询数值大于中间值，则说明要查询的数值在后半部分，那么继续二分后半部分；当要查询数值小于中间数值时，说明要查询的数值在前半部分，那么继续二分前半部分。（过滤掉一半数据查询）</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 递归实现</span></span><br><span class="line"><span class="comment">     * array $arr 有序数组</span></span><br><span class="line"><span class="comment">     * int $search 要查询的数字</span></span><br><span class="line"><span class="comment">     * int firstIndex 数组起始位置</span></span><br><span class="line"><span class="comment">     * int lastIndex 数组结束位置</span></span><br><span class="line"><span class="comment">     * @return </span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">binarySearchRecursion</span>(<span class="params">array $arr, $search, $lastIndex, $firstIndex = <span class="number">0</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $len = count($arr);</span><br><span class="line">        <span class="keyword">if</span> ($len &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        $middle = intval(($firstIndex + $lastIndex) / <span class="number">2</span>);</span><br><span class="line">        <span class="keyword">if</span> ($search == $arr[$middle]) &#123;<span class="comment">//找到直接返回</span></span><br><span class="line">            <span class="keyword">return</span> $arr[$middle];</span><br><span class="line">        &#125; elseif ($search &gt; $arr[$middle]) &#123;<span class="comment">//去后面查，数组起始位置变为$middle + 1。</span></span><br><span class="line">            <span class="keyword">return</span> binarySearchRecursion($arr, $search, $middle + <span class="number">1</span>, $lastIndex);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;<span class="comment">//去前面查，数组结束位置为$middle - 1。</span></span><br><span class="line">            <span class="keyword">return</span> binarySearchRecursion($arr, $search, $firstIndex, $middle - <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 递归实现</span></span><br><span class="line"><span class="comment">     * array $arr 有序数组</span></span><br><span class="line"><span class="comment">     * int $search 要查询的数字</span></span><br><span class="line"><span class="comment">     * @return </span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">     <span class="function"><span class="keyword">function</span> <span class="title">binarySearch</span>(<span class="params">$arr, $search</span>)</span></span><br><span class="line"><span class="function">     </span>&#123;</span><br><span class="line">        $len = count($arr);</span><br><span class="line">        <span class="keyword">if</span> ($len &lt;= <span class="number">0</span> ) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        $firstIndex = <span class="number">0</span>;</span><br><span class="line">        $lastIndex = $len - <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">while</span>($firstIndex &lt;= $lastIndex) &#123;</span><br><span class="line">            $middle = intval(($firstIndex + $lastIndex) / <span class="number">2</span>);</span><br><span class="line">            <span class="keyword">if</span> ($search == $arr[$middle]) &#123;<span class="comment">//出口</span></span><br><span class="line">                <span class="keyword">return</span> $arr[$middle];</span><br><span class="line">            &#125; elseif ($search &gt; $arr[$middle]) &#123;<span class="comment">//去后面查，数组起始位置变为$middle + 1。</span></span><br><span class="line">                $firstIndex = $middle + <span class="number">1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;<span class="comment">//去前面查，数组结束位置为$middle - 1。</span></span><br><span class="line">                $lastIndex = $middle - <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">     &#125;</span><br><span class="line">     https:<span class="comment">//learnku.com/articles/35240</span></span><br></pre></td></tr></table></figure>
<h3 id="子孙树"><a href="#子孙树" class="headerlink" title="子孙树"></a>子孙树</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">$arr = [</span><br><span class="line">    [<span class="string">'id'</span>=&gt;<span class="number">1</span>,<span class="string">'parent_id'</span>=&gt;<span class="number">0</span>,<span class="string">'type'</span>=&gt;<span class="number">5</span>],</span><br><span class="line">    [<span class="string">'id'</span>=&gt;<span class="number">2</span>,<span class="string">'parent_id'</span>=&gt;<span class="number">1</span>,<span class="string">'type'</span>=&gt;<span class="number">3</span>],</span><br><span class="line">    [<span class="string">'id'</span>=&gt;<span class="number">3</span>,<span class="string">'parent_id'</span>=&gt;<span class="number">0</span>,<span class="string">'type'</span>=&gt;<span class="number">1</span>],</span><br><span class="line">    [<span class="string">'id'</span>=&gt;<span class="number">4</span>,<span class="string">'parent_id'</span>=&gt;<span class="number">3</span>,<span class="string">'type'</span>=&gt;<span class="number">3</span>],</span><br><span class="line">];</span><br><span class="line">var_dump(getSubTree($arr));</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getSubTree</span>(<span class="params">$data, $parent = <span class="string">'parent_id'</span>, $son = <span class="string">'id'</span>, $pid = <span class="number">0</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $tmp = [];</span><br><span class="line">        foreach ($data <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($value[$parent] == $pid) &#123;</span><br><span class="line">                $value[<span class="string">'child'</span>] = getSubTree($data, $parent, $son, $value[$son]);</span><br><span class="line">                $tmp[] = $value;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $tmp;</span><br><span class="line">    &#125;</span><br><span class="line">    http:<span class="comment">//www.putyy.com/article/42</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-的设计哲学"><a href="#Laravel-的设计哲学" class="headerlink" title="Laravel 的设计哲学"></a>Laravel 的设计哲学</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $users = User::all();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> view(<span class="string">'users.index'</span>, compact(<span class="string">'users'</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">改为 &lt;?php</span><br><span class="line">   </span><br><span class="line">   namespace App\Repositories;</span><br><span class="line">   </span><br><span class="line">   use App\User;</span><br><span class="line">   </span><br><span class="line">   <span class="class"><span class="keyword">class</span> <span class="title">DbUserRepository</span></span></span><br><span class="line"><span class="class">   </span>&#123;</span><br><span class="line">       public <span class="function"><span class="keyword">function</span> <span class="title">all</span>(<span class="params"></span>): <span class="title">array</span></span></span><br><span class="line"><span class="function">       </span>&#123;</span><br><span class="line">           <span class="keyword">return</span> User::all()-&gt;toArray();</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class">   </span>&#123;</span><br><span class="line">       private $users;</span><br><span class="line">   </span><br><span class="line">       public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"> </span>)</span></span><br><span class="line"><span class="function">       </span>&#123;</span><br><span class="line">           $<span class="keyword">this</span>-&gt;users = <span class="keyword">new</span> DbUserRepository;</span><br><span class="line">           <span class="comment">// $this-&gt;users = new RedisRepository;</span></span><br><span class="line">       &#125;</span><br><span class="line">   </span><br><span class="line">       public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">       </span>&#123;   </span><br><span class="line">           $users = $<span class="keyword">this</span>-&gt;users-&gt;all();</span><br><span class="line">           <span class="keyword">return</span> $users;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">   依赖正转的不合理之处在哪里呢？位于高层的控制器依赖于具体的底层数据获取服务，当底层发生变动时，就需要对应的修改高层的内部结构。</span><br><span class="line">   </span><br><span class="line">   我们对依赖关系进一步分析，可知控制器关注的并不是具体如何获取数据，控制器关注的是「数据的可获取性」这一抽象。因此，我们应当将依赖关系进行反转，将对依赖的具体声明职责转移到外部，让控制器仅依赖于抽象层（数据的可获取性）。这种解决方式称之为 控制反转 或 依赖倒置。通过控制反转，高层不再依赖于具体的底层，仅仅是依赖于抽象层，高层和底层实现了解耦。</span><br><span class="line">   用接口来表示「数据的可获取性」这一抽象</span><br><span class="line">   </span><br><span class="line">   &lt;?php</span><br><span class="line">   </span><br><span class="line">   namespace App\Repositories;</span><br><span class="line">   </span><br><span class="line">   interface UserRepositoryInterface</span><br><span class="line">   &#123;</span><br><span class="line">       public <span class="function"><span class="keyword">function</span> <span class="title">all</span>(<span class="params"></span>): <span class="title">array</span>;</span></span><br><span class="line"><span class="function">   &#125;</span></span><br><span class="line"><span class="function">   <span class="title">UserController</span> 依赖的是「数据的可获取性」，不依赖于具体的实现</span></span><br><span class="line"><span class="function">   </span></span><br><span class="line"><span class="function">   <span class="title">class</span> <span class="title">UserController</span> <span class="title">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="function">   </span>&#123;</span><br><span class="line">       private $users;</span><br><span class="line">   </span><br><span class="line">       public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">UserRepositoryInterface $users</span>)</span></span><br><span class="line"><span class="function">       </span>&#123;</span><br><span class="line">           $<span class="keyword">this</span>-&gt;users = $users;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">   具体的实现交给对应的仓库类即可</span><br><span class="line">   </span><br><span class="line">   <span class="class"><span class="keyword">class</span> <span class="title">DbUserRepository</span> <span class="title">implements</span> <span class="title">UserRepositoryInterface</span> </span>&#123;&#125;</span><br><span class="line">   <span class="class"><span class="keyword">class</span> <span class="title">RedisRepository</span> <span class="title">implements</span> <span class="title">UserRepositoryInterface</span> </span>&#123; &#125;</span><br><span class="line">   根据自己的需要注入对应的服务，这样就实现了依赖注入。</span><br><span class="line">   </span><br><span class="line">   $userRepository = <span class="keyword">new</span> DbUserRepository;</span><br><span class="line">   $userController = <span class="keyword">new</span> UserController($userRepository)</span><br><span class="line">   总的来说，依赖注入由四部分构成</span><br><span class="line">   </span><br><span class="line">   被使用的服务 - DbUserRepository 或者 RedisRepository 等</span><br><span class="line">   依赖某种服务的客户端 - UserController</span><br><span class="line">   声明客户端如何依赖服务的接口 - UserRepositoryInterface</span><br><span class="line">   依赖注入器，用于决定注入哪项服务给客户端</span><br><span class="line">   </span><br><span class="line">   一个简单的服务容器的实现</span><br><span class="line">   </span><br><span class="line">   namespace App\Services;</span><br><span class="line">   </span><br><span class="line">   use Exception;</span><br><span class="line">   </span><br><span class="line">   <span class="class"><span class="keyword">class</span> <span class="title">Container</span> </span></span><br><span class="line"><span class="class">   </span>&#123;</span><br><span class="line">       protected <span class="keyword">static</span> $container = [];</span><br><span class="line">   </span><br><span class="line">       <span class="comment">/**</span></span><br><span class="line"><span class="comment">        * 绑定服务</span></span><br><span class="line"><span class="comment">        * </span></span><br><span class="line"><span class="comment">        * @param  服务名称 $name </span></span><br><span class="line"><span class="comment">        * @param  Callable $resolver</span></span><br><span class="line"><span class="comment">        * @return void</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">       public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">bind</span>(<span class="params">$name, Callable $resolver</span>)</span></span><br><span class="line"><span class="function">       </span>&#123;   </span><br><span class="line">           <span class="keyword">static</span>::$container[$name] = $resolver;</span><br><span class="line">       &#125;</span><br><span class="line">   </span><br><span class="line">       <span class="comment">/**</span></span><br><span class="line"><span class="comment">        * 解析服务</span></span><br><span class="line"><span class="comment">        * </span></span><br><span class="line"><span class="comment">        * @param  服务名称 $name</span></span><br><span class="line"><span class="comment">        * @return mix</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">       public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">make</span>(<span class="params">$name</span>)</span></span><br><span class="line"><span class="function">       </span>&#123;</span><br><span class="line">           <span class="keyword">if</span>(isset(<span class="keyword">static</span>::$container[$name]))&#123;</span><br><span class="line">               $resolver = <span class="keyword">static</span>::$container[$name];</span><br><span class="line">               <span class="keyword">return</span> $resolver();</span><br><span class="line">           &#125;</span><br><span class="line">   </span><br><span class="line">           <span class="keyword">throw</span> <span class="keyword">new</span> Exception(<span class="string">"不存在该绑定"</span>);</span><br><span class="line">      &#125;</span><br><span class="line">   </span><br><span class="line">   &#125;</span><br><span class="line">   绑定服务</span><br><span class="line">   </span><br><span class="line">   App\Services\Container::bind(<span class="string">'UserRepository'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">       <span class="keyword">return</span> <span class="keyword">new</span> App\Repositories\DbUserRepository;</span><br><span class="line">   &#125;);</span><br><span class="line">   解析服务</span><br><span class="line">   </span><br><span class="line">   $userRepository = App\Services\Container::make(<span class="string">'UserRepository'</span>);</span><br><span class="line">   $userController = <span class="keyword">new</span> UserController($userRepository)</span><br><span class="line">   Laravel 的服务容器的功能则更加的强大，比如，可以将接口与具体的实现进行绑定，通常在 服务提供者 中使用服务容器来进行绑定</span><br><span class="line">   </span><br><span class="line">   public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">   </span>&#123;</span><br><span class="line">       $<span class="keyword">this</span>-&gt;app-&gt;singleton(UserRepositoryInterface::<span class="class"><span class="keyword">class</span>, <span class="title">function</span> ($<span class="title">app</span>) </span>&#123;</span><br><span class="line">           <span class="keyword">return</span> <span class="keyword">new</span> UserRepository;</span><br><span class="line">       &#125;);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<h3 id="高并发业务场景下的秒杀解决方案"><a href="#高并发业务场景下的秒杀解决方案" class="headerlink" title="高并发业务场景下的秒杀解决方案"></a>高并发业务场景下的秒杀解决方案</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">在秒杀前将商品的库存信息加入到 Redis 缓存中。如下格式:</span><br><span class="line">$redis-&gt;lpush(<span class="string">'商品id'</span>,<span class="number">1</span>);</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 1.接受用户请求</span></span><br><span class="line"><span class="comment"> * 2.验证用户是否已经参与秒杀,商品是否存在</span></span><br><span class="line"><span class="comment"> * 3.根据商品id减少商品队列中的库存数量</span></span><br><span class="line"><span class="comment"> * 4.将用户的秒杀数据写入server层中,并返回秒杀数据对应的唯一key值</span></span><br><span class="line"><span class="comment"> * 5.用户点击下单,根据serve层中的缓存数据,生成订单数据并减少数据库商品的库存数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">$getParams = $_POST;</span><br><span class="line">$userId = $getParams[<span class="string">'userId'</span>];</span><br><span class="line">$goodsId = $getParams[<span class="string">'goodsId'</span>];</span><br><span class="line"></span><br><span class="line">$key = <span class="string">'goods:miaosha:'</span>;</span><br><span class="line">$userResult = $redis-&gt;get($key.$userId);</span><br><span class="line"><span class="keyword">if</span>($userResult)&#123;</span><br><span class="line">    $userResult = json_decode($userResult,<span class="literal">true</span>);</span><br><span class="line">    echo json_encode([<span class="string">'result'</span>=&gt;$userResult[<span class="string">'result'</span>],<span class="string">'key'</span>=&gt;$key.$userId]);<span class="comment">// 已经参与过秒杀了</span></span><br><span class="line">    die();</span><br><span class="line">&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    $goodqueue = <span class="string">'goods:queue:'</span>.$goodsId;</span><br><span class="line">    $result = $redis-&gt;lpop($goodqueue);<span class="comment">// 删除商品redis队列缓存</span></span><br><span class="line">    <span class="keyword">if</span>($result)&#123;</span><br><span class="line">        $data = json_encode([<span class="string">'result'</span>=&gt;<span class="string">'OK'</span>,<span class="string">'userId'</span>=&gt;$userId,<span class="string">'goodsId'</span>=&gt;$goodsId]);</span><br><span class="line">        $redis-&gt;set($key.$userId,$data);<span class="comment">// 将秒杀信息写入缓存中</span></span><br><span class="line">        echo json_encode([<span class="string">'result'</span>=&gt;<span class="string">'OK'</span>,<span class="string">'userId'</span>=&gt;$userId,<span class="string">'goodsId'</span>=&gt;$goodsId,<span class="string">'key'</span>=&gt;$key.$userId]);</span><br><span class="line">        die();</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        echo json_encode([<span class="string">'result'</span>=&gt;<span class="string">'FAIL'</span>,<span class="string">'message'</span>=&gt;<span class="string">'商品不存在'</span>,<span class="string">'goodsId'</span>=&gt;$goodsId]);<span class="comment">// 商品库存不存在</span></span><br><span class="line">        die();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;<span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 用户下单界面</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"> require_once __DIR__.<span class="string">'/redis_connect.php'</span>;</span><br><span class="line"> $key = $_GET[<span class="string">'key'</span>];</span><br><span class="line"> $data = $redis-&gt;get($key);</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 生成订单，订单入库</span></span><br><span class="line"><span class="comment">  *</span></span><br><span class="line"><span class="comment">  */</span>https:<span class="comment">//learnku.com/articles/35141</span></span><br><span class="line">redis list 可以批量插入数据，不一定每次都只插入一个值.</span><br><span class="line"></span><br><span class="line">$numberArr = range(<span class="number">1</span>,<span class="number">100</span>);  </span><br><span class="line"><span class="comment">//var_dump($numberArr);</span></span><br><span class="line">$redis-&gt;lPush(<span class="string">'goods:queue:5'</span>,...$numberArr); <span class="comment">// 可变参数</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-开启-Opcache-后如何优雅地部署-PHP-代码"><a href="#PHP-开启-Opcache-后如何优雅地部署-PHP-代码" class="headerlink" title="PHP 开启 Opcache 后如何优雅地部署 PHP 代码"></a>PHP 开启 Opcache 后如何优雅地部署 PHP 代码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">提交了代码并且部署了以后，线上代码依然是旧的。所以我执行了下</span><br><span class="line">/etc/init.d/php-fpm reload 就生效了。</span><br><span class="line">opcache_reset (); 但是注意了 这个函数是强制清楚所有 cache 所以 如果并发较高的系统 不能直接这么搞 还有就是 它有两个清除模式，cli 下只能清除 cli 的 cache fpm 的话 必须通过 fpm 请求方式清除才行</span><br><span class="line">https:<span class="comment">//github.com/gordalina/cachetool</span></span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/35142</span></span><br></pre></td></tr></table></figure>
<h3 id="模型-query-无数据"><a href="#模型-query-无数据" class="headerlink" title="模型::query () 无数据"></a>模型::query () 无数据</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">getPost</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> <span class="keyword">return</span> Post::query()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">   $post = $<span class="keyword">this</span>-&gt;getPost();</span><br><span class="line">    <span class="keyword">return</span> view(<span class="string">'index'</span>, [</span><br><span class="line">    <span class="string">'post'</span> =&gt; $post</span><br><span class="line">    ]);</span><br><span class="line">&#125;</span><br><span class="line">@foreach($post-&gt;where(<span class="string">'category_id'</span>,<span class="number">2</span>)-&gt;get() <span class="keyword">as</span> xxx)&#123;...&#125;</span><br><span class="line">@foreach($post-&gt;where(<span class="string">'category_id'</span>,<span class="number">3</span>)-&gt;get() <span class="keyword">as</span> xxx)&#123;...&#125;</span><br><span class="line">改(clone $post)-&gt;where(xxxxxxxxx)</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/35341</span></span><br></pre></td></tr></table></figure>
<h3 id="JSON-encode-小数位丢失"><a href="#JSON-encode-小数位丢失" class="headerlink" title="JSON_encode 小数位丢失"></a>JSON_encode 小数位丢失</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">$a = <span class="string">'&#123;"orderAmt":500.00&#125;'</span>;</span><br><span class="line">$a_json_decode = json_decode($a,<span class="literal">true</span>);</span><br><span class="line">[                      </span><br><span class="line">  <span class="string">"orderAmt"</span> =&gt; <span class="number">500.0</span>, </span><br><span class="line">]                      </span><br><span class="line">$a_json_encode = json_encode($a_json_decode);</span><br><span class="line"><span class="string">"&#123;"</span>orderAmt<span class="string">":500&#125;"</span></span><br><span class="line">使用字符串类型的 <span class="number">500.00</span>，或使用字符串拼接 <span class="built_in">JSON</span> </span><br><span class="line">json_encode($a_json_decode, JSON_PRESERVE_ZERO_FRACTION)</span><br><span class="line">=&gt; <span class="string">"&#123;"</span>orderAmt<span class="string">":500.0&#125;"</span></span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/35311</span></span><br><span class="line"></span><br><span class="line"> 从 PHP <span class="number">5.6</span><span class="number">.6</span>+ 开始，json_encode 支持使用 JSON_PRESERVE_ZERO_FRACTION 选项以告知引擎确保浮点数始终编码为浮点数，但对于形如 <span class="number">500.00</span> （值为 <span class="number">500</span>，精确到小数点后两位的浮点数）仅能保证最终输出 <span class="number">500.0</span></span><br><span class="line"> </span><br><span class="line"> 即使临时配置 serialize_precision 为 <span class="number">10</span>，在初始化数组时使用 number_format 函数格式化小数位数，最后在 json_encode 时填入选项 JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK 也仅能输出 <span class="number">500.0</span>。</span><br></pre></td></tr></table></figure>
<h3 id="支付宝公钥证书-PHP-版本-SDK"><a href="#支付宝公钥证书-PHP-版本-SDK" class="headerlink" title="支付宝公钥证书 PHP 版本 SDK"></a>支付宝公钥证书 PHP 版本 SDK</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getRootCertSN</span>(<span class="params">$str</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// return '687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6';</span></span><br><span class="line">        $arr = preg_split(<span class="string">'/(?=-----BEGIN)/'</span>, $str, <span class="number">-1</span>, PREG_SPLIT_NO_EMPTY);</span><br><span class="line">        $str = <span class="literal">null</span>;</span><br><span class="line">        foreach ($arr <span class="keyword">as</span> $e) &#123;</span><br><span class="line">            $sn = getCertSN($e, <span class="literal">true</span>);</span><br><span class="line">            <span class="keyword">if</span> (!$sn) &#123;</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> ($str === <span class="literal">null</span>) &#123;</span><br><span class="line">                $str = $sn;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                $str .= <span class="string">"_"</span> . $sn;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $str;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">getCertSN</span>(<span class="params">$str, $matchAlgo = false</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        根据java SDK源码：AntCertificationUtil::getRootCertSN</span></span><br><span class="line"><span class="comment">        对证书链中RSA的项目进行过滤（猜测是gm国密算法java抛错搞不定，故意略去）</span></span><br><span class="line"><span class="comment">        java源码为：</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        if(c.getSigAlgOID().startsWith("1.2.840.113549.1.1"))</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        根据 https://www.alvestrand.no/objectid/1.2.840.113549.1.1.html</span></span><br><span class="line"><span class="comment">        该OID为RSA算法系。</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        <span class="keyword">if</span> ($matchAlgo) &#123;</span><br><span class="line">            openssl_x509_export($str, $out, <span class="literal">false</span>);</span><br><span class="line">            <span class="keyword">if</span> (!preg_match(<span class="string">'/Signature Algorithm:.*?RSA/im'</span>, $out, $m)) &#123;</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        $a = openssl_x509_parse($str);</span><br><span class="line">        $issuer = <span class="literal">null</span>;</span><br><span class="line">        <span class="comment">// 注意：根据java代码输出，需要倒着排列 CN,OU,O</span></span><br><span class="line">        foreach ($a[<span class="string">"issuer"</span>] <span class="keyword">as</span> $k =&gt; $v) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($issuer === <span class="literal">null</span>) &#123;</span><br><span class="line">                $issuer = <span class="string">"$k=$v"</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                $issuer = <span class="string">"$k=$v,"</span> . $issuer;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        #    echo($issuer . $a["serialNumber"] . "\n");</span><br><span class="line">        $serialNumberHex = decimalNotation($a[<span class="string">'serialNumberHex'</span>]);</span><br><span class="line">        $sn = md5($issuer . $serialNumberHex);</span><br><span class="line">        <span class="keyword">return</span> $sn;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">decimalNotation</span>(<span class="params">$hex</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $dec = <span class="number">0</span>;</span><br><span class="line">        $len = strlen($hex);</span><br><span class="line">        <span class="keyword">for</span> ($i = <span class="number">1</span>; $i &lt;= $len; $i++) &#123;</span><br><span class="line">            $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - <span class="number">1</span>])), bcpow(<span class="string">'16'</span>, strval($len - $i))));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $dec;</span><br><span class="line">    &#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/35315</span></span><br></pre></td></tr></table></figure>
<h3 id="字符串表达式计算"><a href="#字符串表达式计算" class="headerlink" title="字符串表达式计算"></a>字符串表达式计算</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">$a = <span class="number">10</span>;</span><br><span class="line">var_dump(<span class="built_in">eval</span>(<span class="string">'return $a &gt; 5;'</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 输出:</span></span><br><span class="line"><span class="comment">// bool(true)</span></span><br><span class="line">system(<span class="string">'php -r "echo 1 + 2;"'</span>);</span><br><span class="line"></span><br><span class="line">echo exec(<span class="string">'php -r "echo 1 + 2;"'</span>);</span><br><span class="line">https:<span class="comment">//shockerli.net/post/php-expression-string/</span></span><br></pre></td></tr></table></figure>
<h3 id="用户登录密码改为-md5"><a href="#用户登录密码改为-md5" class="headerlink" title="用户登录密码改为 md5"></a>用户登录密码改为 md5</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br></pre></td><td class="code"><pre><span class="line">新建一个文件 Libraries，在 Libraries 目录下新建一个 MD5.php 文件</span><br><span class="line"></span><br><span class="line">namespace App\Libraries; </span><br><span class="line"></span><br><span class="line">use Illuminate\Contracts\Hashing\Hasher; </span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MD5</span> <span class="title">implements</span> <span class="title">Hasher</span> </span></span><br><span class="line"><span class="class"></span>&#123; </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * Hash the given value. </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @param string $value </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @return array  $options </span></span><br><span class="line"><span class="comment">     * @return string </span></span><br><span class="line"><span class="comment">     */</span> </span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">make</span>(<span class="params">$value, array $options = []</span>) </span></span><br><span class="line"><span class="function">    </span>&#123; </span><br><span class="line">        <span class="keyword">return</span> md5($value); </span><br><span class="line">    &#125; </span><br><span class="line"></span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * Check the given plain value against a hash. </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @param string $value </span></span><br><span class="line"><span class="comment">     * @param string $hashedValue </span></span><br><span class="line"><span class="comment">     * @param array $options </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @return bool </span></span><br><span class="line"><span class="comment">     */</span> </span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">check</span>(<span class="params">$value, $hashedValue, array $options = []</span>) </span></span><br><span class="line"><span class="function">    </span>&#123; </span><br><span class="line">        <span class="keyword">if</span>(empty($hashedValue))&#123; </span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>; </span><br><span class="line">        &#125; </span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;make($value) === $hashedValue; </span><br><span class="line">    &#125; </span><br><span class="line"></span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * Check if the given hash has been hashed using the given options. </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @param string $hashedValue </span></span><br><span class="line"><span class="comment">     * @param array $options </span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @return bool </span></span><br><span class="line"><span class="comment">     */</span> </span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">needsRehash</span>(<span class="params">$hashedValue, array $options = []</span>) </span></span><br><span class="line"><span class="function">    </span>&#123; </span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>; </span><br><span class="line">    &#125; </span><br><span class="line">&#125; </span><br><span class="line"> Providers 文件下面新建一个文件 MD5ServiceProvider.php</span><br><span class="line"> namespace App\Providers; </span><br><span class="line"> </span><br><span class="line"> use Illuminate\Auth\EloquentUserProvider; </span><br><span class="line"> <span class="class"><span class="keyword">class</span> <span class="title">MD5ServiceProvider</span> <span class="keyword">extends</span> <span class="title">EloquentUserProvider</span> </span></span><br><span class="line"><span class="class"> </span>&#123; </span><br><span class="line"> </span><br><span class="line">     <span class="comment">//继承EloquentUserProvider类，调用父类的构造函数 </span></span><br><span class="line">     public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$hasher, $model</span>) </span></span><br><span class="line"><span class="function">     </span>&#123; </span><br><span class="line">         parent::__construct($hasher, $model); </span><br><span class="line">     &#125; </span><br><span class="line"> </span><br><span class="line">     <span class="comment">/** </span></span><br><span class="line"><span class="comment">      * Bootstrap the application services. </span></span><br><span class="line"><span class="comment">      * </span></span><br><span class="line"><span class="comment">      * @return void </span></span><br><span class="line"><span class="comment">      */</span> </span><br><span class="line">     public <span class="function"><span class="keyword">function</span> <span class="title">boot</span>(<span class="params"></span>) </span></span><br><span class="line"><span class="function">     </span>&#123; </span><br><span class="line">         <span class="comment">// </span></span><br><span class="line">     &#125; </span><br><span class="line"> </span><br><span class="line">     <span class="comment">/** </span></span><br><span class="line"><span class="comment">      * Register the application services. </span></span><br><span class="line"><span class="comment">      * </span></span><br><span class="line"><span class="comment">      * @return void </span></span><br><span class="line"><span class="comment">      */</span> </span><br><span class="line">     public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>) </span></span><br><span class="line"><span class="function">     </span>&#123; </span><br><span class="line">         <span class="comment">// </span></span><br><span class="line">     &#125; </span><br><span class="line"> &#125; </span><br><span class="line">  AuthServiceProvider.php 文件里 boot 方法里添加如下代码</span><br><span class="line"> </span><br><span class="line">         Auth::provider(<span class="string">'MD5'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$app</span>) </span>&#123; </span><br><span class="line">             $model = config(<span class="string">'auth.providers.users.model'</span>); </span><br><span class="line">             <span class="keyword">return</span> <span class="keyword">new</span> MD5ServiceProvider(<span class="keyword">new</span> MD5, $model); </span><br><span class="line">         &#125;); </span><br><span class="line">修改 config/auth.php 里的 providers</span><br><span class="line"><span class="string">'providers'</span> =&gt; [</span><br><span class="line">        <span class="string">'users'</span> =&gt; [</span><br><span class="line">            <span class="string">'driver'</span> =&gt; <span class="string">'MD5'</span>,<span class="comment">//'driver' =&gt; 'eloquent',//eloquent默认加密码方式</span></span><br><span class="line">            <span class="string">'model'</span> =&gt; App\User::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">        ],</span><br><span class="line">        <span class="string">'admins'</span> =&gt; [</span><br><span class="line">            <span class="string">'driver'</span> =&gt; <span class="string">'eloquent'</span>,</span><br><span class="line">            <span class="string">'model'</span> =&gt; App\Admin::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">        ],</span><br><span class="line">        修改 app/Http/Controllers/Auth/RegisterController.php 里的 create，修改代码如下</span><br><span class="line">        </span><br><span class="line">            protected <span class="function"><span class="keyword">function</span> <span class="title">create</span>(<span class="params">array $data</span>)</span></span><br><span class="line"><span class="function">            </span>&#123;</span><br><span class="line">                <span class="keyword">return</span> User::create([</span><br><span class="line">                    <span class="string">'name'</span> =&gt; $data[<span class="string">'name'</span>],</span><br><span class="line">                    <span class="string">'email'</span> =&gt; $data[<span class="string">'email'</span>],</span><br><span class="line">                    <span class="string">'password'</span> =&gt; md5($data[<span class="string">'password'</span>]),</span><br><span class="line">                ]);</span><br><span class="line">            &#125;</span><br><span class="line">            https:<span class="comment">//learnku.com/articles/35407</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel跨库跨连接的事务操作"><a href="#Laravel跨库跨连接的事务操作" class="headerlink" title="Laravel跨库跨连接的事务操作"></a>Laravel跨库跨连接的事务操作</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">Laravel 下的跨库事务操作是基于连接的 当执行 DB::beginTransaction(); 的时候 其实是和默认的数据库配置建立了连接 后面的操作 commit 或者 rollback 都是操作的这个默认数据库 如果在这中间操作了其他的数据库 对他是不生效的</span><br><span class="line">同时 commit 和 rollback 都 指定连接</span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">    <span class="comment">//开启默认数据库的事务</span></span><br><span class="line">    DB::beginTransaction();</span><br><span class="line">    <span class="comment">//开启test数据库的事务</span></span><br><span class="line">    DB::connection(<span class="string">'test'</span>)-&gt;beginTransaction();</span><br><span class="line">    <span class="comment">//中间各种数据库操作</span></span><br><span class="line">    Table1::xxxxxx();</span><br><span class="line">    Table2::xxxxxx();</span><br><span class="line">    <span class="keyword">if</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">        <span class="comment">//一起提交</span></span><br><span class="line">        DB::commit();</span><br><span class="line">        DB::connection(<span class="string">'test'</span>)-&gt;commit();</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">//一起回滚</span></span><br><span class="line">        DB::rollback();</span><br><span class="line">        DB::connection(<span class="string">'test'</span>)-&gt;rollback();</span><br><span class="line">    &#125;</span><br><span class="line">&#125; <span class="keyword">catch</span> (\Exception $exception) &#123;</span><br><span class="line">    echo <span class="string">"catch some errors:"</span>.$exception-&gt;getMessage();</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//caihongtengxu.github.io/2018/20181009/index.html</span></span><br></pre></td></tr></table></figure>
<h3 id="array-splice无法自定义键值"><a href="#array-splice无法自定义键值" class="headerlink" title="array_splice无法自定义键值"></a>array_splice无法自定义键值</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">array_insert</span> (<span class="params">&amp;$array, $position, $insert_array</span>) </span>&#123;</span><br><span class="line">            $first_array = array_splice ($array, <span class="number">0</span>, $position);</span><br><span class="line">            $array = array_merge ($first_array, $insert_array, $array);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        $arr = array(</span><br><span class="line">            <span class="string">'tt'</span> =&gt; <span class="number">1333</span>,</span><br><span class="line">            <span class="string">'cc'</span> =&gt; <span class="number">333</span>,</span><br><span class="line">            <span class="string">'aaz'</span> =&gt; <span class="number">2333</span>,</span><br><span class="line">            <span class="string">'ee'</span> =&gt; <span class="number">78</span>,</span><br><span class="line">        );</span><br><span class="line">        $temp[<span class="string">"bb"</span>] = <span class="number">33</span>;</span><br><span class="line">        array_insert($arr,<span class="number">1</span>,$temp);</span><br><span class="line">&gt;&gt;&gt; $arr</span><br><span class="line">=&gt; [</span><br><span class="line">     <span class="string">"tt"</span> =&gt; <span class="number">1333</span>,</span><br><span class="line">     <span class="string">"bb"</span> =&gt; <span class="number">33</span>,</span><br><span class="line">     <span class="string">"cc"</span> =&gt; <span class="number">333</span>,</span><br><span class="line">     <span class="string">"aaz"</span> =&gt; <span class="number">2333</span>,</span><br><span class="line">     <span class="string">"ee"</span> =&gt; <span class="number">78</span>,</span><br><span class="line">   ]</span><br></pre></td></tr></table></figure>
<h3 id="数字转度量"><a href="#数字转度量" class="headerlink" title="数字转度量"></a>数字转度量</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 数字转度量</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @param int $num 数字</span></span><br><span class="line"><span class="comment"> * @return string|int</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">num2metric</span>(<span class="params">$num, $precision = <span class="number">0</span></span>) </span>&#123;</span><br><span class="line">    $unitList = [</span><br><span class="line">        <span class="string">'P'</span> =&gt; <span class="number">15</span>,</span><br><span class="line">        <span class="string">'T'</span> =&gt; <span class="number">12</span>,</span><br><span class="line">        <span class="string">'G'</span> =&gt; <span class="number">9</span>,</span><br><span class="line">        <span class="string">'M'</span> =&gt; <span class="number">6</span>,</span><br><span class="line">        <span class="string">'W'</span> =&gt; <span class="number">4</span>,</span><br><span class="line">        <span class="string">'K'</span> =&gt; <span class="number">3</span>,</span><br><span class="line">    ];</span><br><span class="line"></span><br><span class="line">    $num = (int) $num;</span><br><span class="line"></span><br><span class="line">    foreach($unitList <span class="keyword">as</span> $name =&gt; $pow) &#123;</span><br><span class="line">        $size = pow(<span class="number">10</span>, $pow);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>($num &gt;= $size) &#123;</span><br><span class="line">            <span class="keyword">return</span> round($num / $size * <span class="number">100</span>, $precision) / <span class="number">100</span> . $name;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $num;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//www.hongfs.cn/2018/10/php/php-metric-prefix/</span></span><br><span class="line">num2metric(<span class="number">1000</span>) <span class="comment">// 1K</span></span><br><span class="line">num2metric(<span class="string">'10000'</span>) <span class="comment">// 1W</span></span><br><span class="line">num2metric(<span class="string">'A'</span>) <span class="comment">// 0</span></span><br></pre></td></tr></table></figure>
<h3 id="获取两个坐标之间距离"><a href="#获取两个坐标之间距离" class="headerlink" title="获取两个坐标之间距离"></a>获取两个坐标之间距离</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取两个坐标之间距离</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @param int|float $lat1 第一个坐标纬度</span></span><br><span class="line"><span class="comment"> * @param int|float $lon1 第一个坐标经度</span></span><br><span class="line"><span class="comment"> * @param int|float $lat2 第一个坐标纬度</span></span><br><span class="line"><span class="comment"> * @param int|float $lon2 第二个坐标经度</span></span><br><span class="line"><span class="comment"> * @param string $unit 距离单位 M 法定英里 K 公里 N 海里</span></span><br><span class="line"><span class="comment"> * @return int|float</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getDistanceBetweenPoints</span>(<span class="params">$lat1, $lon1, $lat2, $lon2, $unit = <span class="string">'K'</span></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (($lat1 == $lat2) &amp;&amp; ($lon1 == $lon2)) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    $theta = $lon1 - $lon2;</span><br><span class="line">    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));</span><br><span class="line">    $dist = acos($dist);</span><br><span class="line">    $dist = rad2deg($dist);</span><br><span class="line">    $miles = $dist * <span class="number">60</span> * <span class="number">1.1515</span>;</span><br><span class="line">    $unit = strtoupper($unit);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> ($unit === <span class="string">'K'</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> $miles * <span class="number">1.609344</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> ($unit === <span class="string">'N'</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> $miles * <span class="number">0.8684</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> $miles;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;https:<span class="comment">//www.hongfs.cn/2019/06/php/php-get-distance-between-points/</span></span><br><span class="line">getDistanceBetweenPoints(<span class="number">113.276885</span>, <span class="number">23.090654</span>, <span class="number">113.320331</span>, <span class="number">23.096197</span>);</span><br><span class="line"><span class="comment">// PHP: 4.8368890520256 公里</span></span><br><span class="line"><span class="comment">// 高德地图: 4491 米</span></span><br><span class="line"><span class="comment">//restapi.amap.com/v3/distance？key-您的key&amp;origins=113.276885，23.090654&amp;destination=113.320331，23.096197&amp;type=e</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-上传图片"><a href="#Laravel-上传图片" class="headerlink" title="Laravel 上传图片"></a>Laravel 上传图片</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 后缀名验证</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @param  string  $extension  后缀名</span></span><br><span class="line"><span class="comment"> * @return bool</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">protected <span class="function"><span class="keyword">function</span> <span class="title">has_extension</span>(<span class="params">string $extension</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> in_array($extension, [<span class="string">'jpg'</span>, <span class="string">'jpeg'</span>, <span class="string">'png'</span>, <span class="string">'gif'</span>, <span class="string">'bmp'</span>]);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 图片上传</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @param  \Illuminate\Http\Request  $request</span></span><br><span class="line"><span class="comment"> * @return \Illuminate\Http\Response</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params">Request $request</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 获取上传键值</span></span><br><span class="line">    $name = $request-&gt;input(<span class="string">'name'</span>, <span class="string">'file'</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>($request-&gt;hasFile($name)) &#123;</span><br><span class="line">        <span class="comment">// 文件格式上传</span></span><br><span class="line"></span><br><span class="line">        $file = $request-&gt;file($name);</span><br><span class="line">        $extension = $file-&gt;extension();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(!$<span class="keyword">this</span>-&gt;has_extension($extension)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'格式错误'</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $filename = $file-&gt;store();</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span>(preg_match(<span class="string">'/^(data:\s*image\/(\w+);base64,)/'</span>, $request-&gt;input($name), $matches)) &#123;</span><br><span class="line">        <span class="comment">// base64 上传</span></span><br><span class="line"></span><br><span class="line">        $extension = $matches[<span class="number">2</span>];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(!$<span class="keyword">this</span>-&gt;has_extension($extension)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'格式错误'</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $file = $request-&gt;input($name);</span><br><span class="line">        $file = preg_replace(<span class="string">'/^(data:\s*image\/(\w+);base64,)/'</span>, <span class="string">''</span>, $file);</span><br><span class="line">        $file = str_replace(<span class="string">' '</span>, <span class="string">'+'</span>, $file);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 生成保存文件名</span></span><br><span class="line">        $filename = str_random(<span class="number">40</span>) . <span class="string">'.'</span> . $extension;</span><br><span class="line">        Storage::put($filename, base64_decode($file));</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'格式错误'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="string">'上传成功['</span> . $filename . <span class="string">']'</span>;</span><br><span class="line">&#125;https:<span class="comment">//www.hongfs.cn/2019/06/php/laravel/laravel-upload-image/</span></span><br></pre></td></tr></table></figure>
<h3 id="拼音排序"><a href="#拼音排序" class="headerlink" title="拼音排序"></a>拼音排序</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">$ composer <span class="built_in">require</span> <span class="string">"overtrue/pinyin:~4.0"</span></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 地区列表 - 拼音排序</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return \Illuminate\Http\Response</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">list</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $list_tmp = DB::table(<span class="string">'area'</span>)</span><br><span class="line">                        -&gt;select(<span class="string">'code'</span>, <span class="string">'name'</span>)</span><br><span class="line">                        -&gt;orderBy(DB::raw(<span class="string">'convert(name using gbk)'</span>))</span><br><span class="line">                        -&gt;get();</span><br><span class="line"></span><br><span class="line">        $pinyin = <span class="keyword">new</span> Pinyin();</span><br><span class="line"></span><br><span class="line">        $list = [];</span><br><span class="line"></span><br><span class="line">        $list_tmp-&gt;map(<span class="function"><span class="keyword">function</span>(<span class="params">$item</span>) <span class="title">use</span>(<span class="params">&amp;$list, $pinyin</span>) </span>&#123;</span><br><span class="line">            <span class="comment">// 去除城市名最后面的市字</span></span><br><span class="line">            <span class="comment">// if(substr($item-&gt;name, -3) === '市') &#123;</span></span><br><span class="line">            <span class="comment">//     $item-&gt;name = substr($item-&gt;name, 0, -3);</span></span><br><span class="line">            <span class="comment">// &#125;</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">// 获取城市名第一个字</span></span><br><span class="line">            $name_first = substr($item-&gt;name, <span class="number">0</span>, <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 获取拼音</span></span><br><span class="line">            $name_pinyin = $pinyin-&gt;convert($name_first)[<span class="number">0</span>];</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 获取第一个字母并且转换为大写</span></span><br><span class="line">            $name_pinyin = strtoupper(substr($name_pinyin, <span class="number">0</span>, <span class="number">1</span>));</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span>(!isset($list[$name_pinyin])) &#123;</span><br><span class="line">                $list[$name_pinyin] = [];</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            $list[$name_pinyin][] = (array) $item;</span><br><span class="line">        &#125;);</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">"code"</span>: <span class="number">1</span>,</span><br><span class="line">            <span class="string">"data"</span>: &#123;</span><br><span class="line">                <span class="string">"G"</span>: [</span><br><span class="line">                    &#123;</span><br><span class="line">                        <span class="string">"code"</span>: <span class="number">5108</span>,</span><br><span class="line">                        <span class="string">"name"</span>: <span class="string">"广元市"</span></span><br><span class="line">                    &#125;,</span><br><span class="line">                    &#123;</span><br><span class="line">                        <span class="string">"code"</span>: <span class="number">4401</span>,</span><br><span class="line">                        <span class="string">"name"</span>: <span class="string">"广州市"</span></span><br><span class="line">                    &#125;</span><br><span class="line">                ],</span><br><span class="line">                <span class="string">"S"</span>: [</span><br><span class="line">                    &#123;</span><br><span class="line">                        <span class="string">"code"</span>: <span class="number">4403</span>,</span><br><span class="line">                        <span class="string">"name"</span>: <span class="string">"深圳市"</span></span><br><span class="line">                    &#125;</span><br><span class="line">                ]</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">https:<span class="comment">//www.hongfs.cn/2019/09/php/laravel/laravel-area-list-pinyin-sort/</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP读取微信客户端数据库"><a href="#PHP读取微信客户端数据库" class="headerlink" title="PHP读取微信客户端数据库"></a>PHP读取微信客户端数据库</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">  <span class="class"><span class="keyword">class</span> <span class="title">MyDB</span> <span class="keyword">extends</span> <span class="title">SQLite3</span></span></span><br><span class="line"><span class="class">   </span>&#123;</span><br><span class="line">      <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">      </span>&#123;</span><br><span class="line">      <span class="comment">//根据sqlite提供的open接口，输入密钥key，</span></span><br><span class="line">      <span class="comment">//$this-&gt;open('EnMicroMsg.db',SQLITE3_OPEN_READWRITE,'71ca1d4');</span></span><br><span class="line">         $<span class="keyword">this</span>-&gt;open(<span class="string">'test.db'</span>);<span class="comment">//test.db存在就链接，不存在就创建</span></span><br><span class="line">      &#125;</span><br><span class="line">   &#125;</span><br><span class="line">   $db = <span class="keyword">new</span> MyDB();</span><br><span class="line">   <span class="keyword">if</span>(!$db)&#123;</span><br><span class="line">      echo $db-&gt;lastErrorMsg();</span><br><span class="line">   &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      echo <span class="string">"Opened database successfully\n"</span>;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   $sql =&lt;&lt;&lt;EOF</span><br><span class="line">      SELECT * from COMPANY;</span><br><span class="line">EOF;</span><br><span class="line"></span><br><span class="line">   $ret = $db-&gt;query($sql);</span><br><span class="line">   while($row = $ret-&gt;fetchArray(SQLITE3_ASSOC) )&#123;</span><br><span class="line">      echo "ID = ". $row['ID'] . "\n";</span><br><span class="line">      echo "NAME = ". $row['NAME'] ."\n";</span><br><span class="line">      echo "ADDRESS = ". $row['ADDRESS'] ."\n";</span><br><span class="line">      echo "SALARY =  ".$row['SALARY'] ."\n\n";</span><br><span class="line">   &#125;</span><br><span class="line">   echo "Operation done successfully\n";</span><br><span class="line">   $db-&gt;close();</span><br><span class="line">https://learnku.com/laravel/t/35478</span><br></pre></td></tr></table></figure>
<h3 id="PHP7-4-可能会对-Laravel-生态带来的改变"><a href="#PHP7-4-可能会对-Laravel-生态带来的改变" class="headerlink" title="PHP7.4 可能会对 Laravel 生态带来的改变"></a>PHP7.4 可能会对 Laravel 生态带来的改变</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">php <span class="number">7.4</span> alpha 版本已经发布了，新特性很多，不过我只关注了预加载（preload）特性。</span><br><span class="line">我们都知道，laravel include 的文件比较多，会带来较大的磁盘 io, 预加载则直接解决了这个问题。https:<span class="comment">//learnku.com/laravel/t/30557  </span></span><br><span class="line">fpm 基本不会成为你的并发瓶颈，数据库往往率先成为并发的瓶颈。</span><br><span class="line">只有在网络通讯应用中，fpm 才有可能成为瓶颈，但是，fpm 也不能作为通讯组件来使用啊</span><br><span class="line"></span><br><span class="line">开启 preload 后，对于需要加载很多文件的项目来说，性能直接有 <span class="number">13</span>%~<span class="number">16</span>% 的提升，那些加载文件过多的框架和项目将会得到和加载一个文件的项目接近的性能</span><br><span class="line">这个特性，是直击 laravel 的痛点的。</span><br><span class="line">我们都知道，laravel 这个框架，加载了很多文件导致性能比一般框架要慢，这个预加载功能，可以助推 laravel 的普及</span><br><span class="line"></span><br><span class="line">php 的运行模式本来就有 fpm 和 cli 两种，其中 fpm 属于 php 的一种特色，目的是简化 web 开发，从而让开发者专注于创作，而不是处理技术细节。preload 的灵感来自于 java 的 HotSpot（jvm 的黑科技，可以做到平均 gc 时长仅为 <span class="number">1</span> 毫秒），也就是在不增加开发复杂度的情况下，对性能进行 <span class="number">16</span>%～<span class="number">16</span>% 的透明提升，这对于有性能提升需求，但又不想改变 fpm 模式的项目来说意义是很大的。</span><br><span class="line"></span><br><span class="line">如果你的项目规模超大（我对超大的定义是 uv / 日大于 <span class="number">2</span> 亿），如果还继续使用 php 的话，那么就可以用 php 的 cli 模式自定义 http 服务，通过 epool 或者 sellect 实现 io 异步轮询，php 文件直接常驻内存，对于性能的提升（对比于 fpm）通常会有 <span class="number">20</span> 到 <span class="number">80</span> 倍的性能提升。跟此相关的解决方案有 workerman，swoole 等。</span><br><span class="line"></span><br><span class="line">刚才没事跑到 thinkphp 官网上，看到了他们把 workman 作为了框架的一部分，用 workman 启动一个 httpServer，就可以切换到常驻内存模式，在编码上会有一点改变，比如 fpm 模式下的 session，文件上传，和 cli 的就不是一回事了，但是如果项目本身使用了 thinkphp 框架封装好的 seesion 或者文件上传方法，那么就不会有问题，这意味着，如果前期你为了赶进度使用了 fpm 模式，那么到了后期，你仍然有无缝切换到 cli 模式的选择权。</span><br></pre></td></tr></table></figure>
<h3 id="无限极分类"><a href="#无限极分类" class="headerlink" title="无限极分类"></a>无限极分类</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><span class="line">$arrs = [</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">1</span>,</span><br><span class="line">        <span class="string">'parent_id'</span>=&gt;<span class="number">0</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">2</span>,</span><br><span class="line">        <span class="string">'parent_id'</span>=&gt;<span class="number">1</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">3</span>,</span><br><span class="line">        <span class="string">'parent_id'</span>=&gt;<span class="number">2</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">4</span>,</span><br><span class="line">        <span class="string">'parent_id'</span>=&gt;<span class="number">0</span></span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">5</span>,</span><br><span class="line">        <span class="string">'parent_id'</span>=&gt;<span class="number">0</span></span><br><span class="line">    ],</span><br><span class="line">];<span class="function"><span class="keyword">function</span> <span class="title">children</span>(<span class="params">$id,$arrs</span>)</span>&#123;</span><br><span class="line">      $result =[];</span><br><span class="line">      foreach ($arrs <span class="keyword">as</span> $v)&#123;</span><br><span class="line">          <span class="keyword">if</span>($id==$v[<span class="string">'parent_id'</span>])&#123;</span><br><span class="line">              $result[]=$v;</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">return</span> $result;</span><br><span class="line">  &#125;</span><br><span class="line">&gt;&gt;&gt; children(<span class="number">0</span>,$arrs)</span><br><span class="line">=&gt; [</span><br><span class="line">     [</span><br><span class="line">       <span class="string">"id"</span> =&gt; <span class="number">1</span>,</span><br><span class="line">       <span class="string">"parent_id"</span> =&gt; <span class="number">0</span>,</span><br><span class="line">     ],</span><br><span class="line">     [</span><br><span class="line">       <span class="string">"id"</span> =&gt; <span class="number">4</span>,</span><br><span class="line">       <span class="string">"parent_id"</span> =&gt; <span class="number">0</span>,</span><br><span class="line">     ],</span><br><span class="line">     [</span><br><span class="line">       <span class="string">"id"</span> =&gt; <span class="number">5</span>,</span><br><span class="line">       <span class="string">"parent_id"</span> =&gt; <span class="number">0</span>,</span><br><span class="line">     ],</span><br><span class="line">   ]</span><br><span class="line">   </span><br><span class="line">   <span class="function"><span class="keyword">function</span> <span class="title">allChildren</span>(<span class="params">$id,$arrs</span>)</span>&#123;</span><br><span class="line">   </span><br><span class="line">       $result = [];</span><br><span class="line">       $children = children($id,$arrs);<span class="comment">//获取儿子数组</span></span><br><span class="line">       foreach($children <span class="keyword">as</span> $k=&gt;$child)&#123;</span><br><span class="line">           $result[$k]=$child;</span><br><span class="line">           $childResult = allChildren($child[<span class="string">'id'</span>],$arrs);<span class="comment">//获取儿子的儿子的儿子无穷尽也</span></span><br><span class="line">           foreach ($childResult <span class="keyword">as</span> $subChild) &#123;</span><br><span class="line">               $child[<span class="string">'children'</span>][]=$subChild;</span><br><span class="line">               $result[$k] = $child;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">return</span> $result;</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   <span class="comment">//实现类似于children($id,$arrs)方法</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">children</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">           <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(get_class($<span class="keyword">this</span>),<span class="string">'parent_id'</span>);</span><br><span class="line">       &#125;</span><br><span class="line">   <span class="comment">//实现了上面的allChildren($id,$arrs)方法</span></span><br><span class="line">   public <span class="function"><span class="keyword">function</span> <span class="title">getAllChildren</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">       </span>&#123;</span><br><span class="line">           $result = [];</span><br><span class="line">           $children = $<span class="keyword">this</span>-&gt;children;</span><br><span class="line">   </span><br><span class="line">           foreach ($children <span class="keyword">as</span> $child) &#123;</span><br><span class="line">               $result[] = $child;</span><br><span class="line">   </span><br><span class="line">               $childResult = $child-&gt;getAllChildren();</span><br><span class="line">               foreach ($childResult <span class="keyword">as</span> $subChild) &#123;</span><br><span class="line">                   $result[] = $subChild;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">   </span><br><span class="line">           <span class="keyword">return</span> $result;</span><br><span class="line">       &#125;</span><br><span class="line">   测试</span><br><span class="line">   https:<span class="comment">//learnku.com/articles/12466/unlimited-classification#reply52473</span></span><br><span class="line">   Model::find(<span class="number">1</span>)-&gt;getAllChildren();</span><br><span class="line">   public <span class="function"><span class="keyword">function</span> <span class="title">children</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(get_class($<span class="keyword">this</span>), <span class="string">'parent_id'</span> );</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">allChildren</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;children()-&gt;<span class="keyword">with</span>( <span class="string">'allChildren'</span> );</span><br><span class="line">    &#125;</span><br><span class="line">    https:<span class="comment">//github.com/betterde/tree</span></span><br><span class="line">    $categorys =  children::<span class="keyword">with</span>(<span class="string">'allChildren'</span>)-&gt;first();</span><br><span class="line">    $arr = [];</span><br><span class="line">        array_walk_recursive($categories,<span class="function"><span class="keyword">function</span> (<span class="params">$v, $k</span>) <span class="title">use</span>(<span class="params">&amp;$arr</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>($k == <span class="string">'id'</span>)</span><br><span class="line">                $arr[] = $v;</span><br><span class="line">        &#125;);   </span><br><span class="line">        https:<span class="comment">//segmentfault.com/a/1190000010359094</span></span><br><span class="line">        Laravel 的无限级分类插件 https:<span class="comment">//github.com/lazychaser/laravel-nestedset</span></span><br></pre></td></tr></table></figure>
<h3 id="限制请求频率中间件"><a href="#限制请求频率中间件" class="headerlink" title="限制请求频率中间件"></a>限制请求频率中间件</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">use Closure;</span><br><span class="line">use Illuminate\Support\Facades\Cache;</span><br><span class="line">use App\Libs\Xres;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ThrottleRequests</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Handle an incoming request.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @param  \Illuminate\Http\Request  $request</span></span><br><span class="line"><span class="comment">     * @param  \Closure  $next</span></span><br><span class="line"><span class="comment">     * @return mixed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">$request, Closure $next</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $unique = $request-&gt;getClientIp() . $request-&gt;getRequestUri();</span><br><span class="line">        <span class="keyword">if</span> (Cache::has(<span class="string">'ip_list'</span>)) &#123;</span><br><span class="line">            $ip_list = Cache::get(<span class="string">'ip_list'</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $ip_list = array();</span><br><span class="line">        &#125;</span><br><span class="line">        $index = array_search($unique, array_column($ip_list, <span class="string">'ip'</span>));</span><br><span class="line">        <span class="keyword">if</span> (<span class="literal">false</span> === $index) &#123;</span><br><span class="line">            array_push($ip_list, [<span class="string">'ip'</span> =&gt; $unique, <span class="string">'time'</span> =&gt; $<span class="keyword">this</span>-&gt;time()]);</span><br><span class="line">            Cache::put(<span class="string">'ip_list'</span>, $ip_list, <span class="number">1440</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $gap = $<span class="keyword">this</span>-&gt;time() - $ip_list[$index][<span class="string">'time'</span>];</span><br><span class="line">            $ip_list[$index][<span class="string">'time'</span>] = $<span class="keyword">this</span>-&gt;time();</span><br><span class="line">            Cache::put(<span class="string">'ip_list'</span>, $ip_list, <span class="number">1</span>);</span><br><span class="line">            <span class="keyword">if</span> ($gap &lt; <span class="number">1000</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> Xres::error(<span class="string">"请求频率过快"</span>, <span class="number">200</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $next($request);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">time</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> (int)(microtime(<span class="literal">true</span>)*<span class="number">1000</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/35823</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-MongoDB-数据库查询"><a href="#Laravel-MongoDB-数据库查询" class="headerlink" title="Laravel MongoDB 数据库查询"></a>Laravel MongoDB 数据库查询</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> jenssegers/mongodb</span><br><span class="line">$capsule-&gt;getDatabaseManager()-&gt;extend(<span class="string">'mongodb'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$config</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> Jenssegers\Mongodb\Connection($config);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="树状数据结构存储方式"><a href="#树状数据结构存储方式" class="headerlink" title="树状数据结构存储方式"></a>树状数据结构存储方式</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line">CREATE TABLE <span class="string">`categories`</span> (</span><br><span class="line">  <span class="string">`id`</span> int(<span class="number">11</span>) NOT NULL AUTO_INCREMENT,</span><br><span class="line">  <span class="string">`title`</span> char(<span class="number">100</span>) NOT NULL,</span><br><span class="line">  <span class="string">`pid`</span> int(<span class="number">11</span>) DEFAULT <span class="number">0</span>,</span><br><span class="line">  PRIMARY KEY (<span class="string">`id`</span>)</span><br><span class="line">) ENGINE=InnoDB;</span><br><span class="line"></span><br><span class="line">CREATE TABLE <span class="string">`categories`</span> (</span><br><span class="line">  <span class="string">`id`</span> int(<span class="number">11</span>) NOT NULL AUTO_INCREMENT,</span><br><span class="line">  <span class="string">`title`</span> char(<span class="number">100</span>) NOT NULL,</span><br><span class="line">  <span class="string">`lft`</span> int(<span class="number">11</span>) NOT NULL CHECK (lft&gt; <span class="number">0</span>),</span><br><span class="line">  <span class="string">`rgt`</span> int(<span class="number">11</span>) NOT NULL CHECK (rgt&gt; <span class="number">1</span>),</span><br><span class="line">  PRIMARY KEY (<span class="string">`id`</span>)</span><br><span class="line">) ENGINE=InnoDB;</span><br><span class="line"></span><br><span class="line">lft 和 rgt 是作为集合的边界，两者差值越大，则集合越大，里面的元素就越多。</span><br><span class="line">根据子集，查找父级的分类</span><br><span class="line"></span><br><span class="line">SELECT c2.* </span><br><span class="line">  FROM categories <span class="keyword">as</span> c1, categories <span class="keyword">as</span> c2</span><br><span class="line">  WHERE c1.lft BETWEEN c2.lft and c2.rgt </span><br><span class="line">      AND c1.title = <span class="string">'华为'</span>;</span><br><span class="line"></span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">| id | title       | lft | rgt |</span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">|  <span class="number">1</span> | Smartphones |   <span class="number">1</span> |  <span class="number">14</span> |</span><br><span class="line">|  <span class="number">5</span> | Harmony OS  |  <span class="number">10</span> |  <span class="number">13</span> |</span><br><span class="line">|  <span class="number">8</span> | 华为        |  <span class="number">11</span> |  <span class="number">12</span> |</span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">根据父级，查找其底下所有的子集</span><br><span class="line"></span><br><span class="line">SELECT c1.*</span><br><span class="line">   FROM categories AS c1, categories AS c2</span><br><span class="line">  WHERE c1.lft BETWEEN c2.lft AND c2.rgt</span><br><span class="line">    AND c2.title = <span class="string">'Smartphones'</span>;</span><br><span class="line"></span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">| id | title       | lft | rgt |</span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">|  <span class="number">1</span> | Smartphones |   <span class="number">1</span> |  <span class="number">14</span> |</span><br><span class="line">|  <span class="number">3</span> | Android     |   <span class="number">2</span> |   <span class="number">5</span> |</span><br><span class="line">|  <span class="number">4</span> | iOS         |   <span class="number">6</span> |   <span class="number">9</span> |</span><br><span class="line">|  <span class="number">5</span> | Harmony OS  |  <span class="number">10</span> |  <span class="number">13</span> |</span><br><span class="line">|  <span class="number">6</span> | 小米        |   <span class="number">3</span> |   <span class="number">4</span> |</span><br><span class="line">|  <span class="number">7</span> | iPhone      |   <span class="number">7</span> |   <span class="number">8</span> |</span><br><span class="line">|  <span class="number">8</span> | 华为        |  <span class="number">11</span> |  <span class="number">12</span> |</span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">查看各个分类的级别</span><br><span class="line"></span><br><span class="line"> SELECT COUNT(c2.id) AS indentation, c1.title</span><br><span class="line">  FROM categories AS c1, categories AS c2</span><br><span class="line">  WHERE c1.lft BETWEEN c2.lft AND c2.rgt</span><br><span class="line">  GROUP BY c1.title</span><br><span class="line">  ORDER BY c1.lft;</span><br><span class="line"></span><br><span class="line">+-------------+-------------+</span><br><span class="line">| indentation | title       |</span><br><span class="line">+-------------+-------------+</span><br><span class="line">|           <span class="number">1</span> | Smartphones |</span><br><span class="line">|           <span class="number">2</span> | Android     |</span><br><span class="line">|           <span class="number">3</span> | 小米        |</span><br><span class="line">|           <span class="number">2</span> | iOS         |</span><br><span class="line">|           <span class="number">3</span> | iPhone      |</span><br><span class="line">|           <span class="number">2</span> | Harmony OS  |</span><br><span class="line">|           <span class="number">3</span> | 华为        |</span><br><span class="line">+-------------+-------------+</span><br><span class="line">https:<span class="comment">//learnku.com/articles/33630</span></span><br></pre></td></tr></table></figure>
<h3 id="冬令时"><a href="#冬令时" class="headerlink" title="冬令时"></a>冬令时</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getSeason</span>(<span class="params">$timezone</span>)</span>&#123;</span><br><span class="line">    $localzone = date(<span class="string">"e"</span>);</span><br><span class="line">    date_default_timezone_set($timezone);</span><br><span class="line">    $season = date(<span class="string">"I"</span>);</span><br><span class="line">    date_default_timezone_set($localzone);</span><br><span class="line">    <span class="keyword">return</span> $season;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">is_dst</span>(<span class="params">$timestamp,$timezone</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $timezone = date(<span class="string">'e'</span>); <span class="comment">//获取当前使用的时区</span></span><br><span class="line">    date_default_timezone_set($timezone); <span class="comment">//强制设置时区</span></span><br><span class="line">    $dst = date(<span class="string">'I'</span>,$timestamp); <span class="comment">//判断是否夏令时</span></span><br><span class="line">    date_default_timezone_set($timezone); <span class="comment">//还原时区</span></span><br><span class="line">    <span class="keyword">return</span> $dst; <span class="comment">//返回结果</span></span><br><span class="line">&#125;</span><br><span class="line">print_r(is_dst(<span class="number">1570143661</span>,<span class="string">'Europe/London'</span>));</span><br><span class="line">print_r(is_dst(time(),<span class="string">'America/New_York'</span>));</span><br><span class="line">print_r(getSeason(<span class="string">'America/Chicago'</span>));</span><br><span class="line">https:<span class="comment">//3v4l.org/iWVMb</span></span><br></pre></td></tr></table></figure>
<h3 id="展开表达式"><a href="#展开表达式" class="headerlink" title="展开表达式"></a>展开表达式</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">$a, $b, $c</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $a + $b + $c;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$operators = [<span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line">echo add(<span class="number">1</span>, ...$operators);</span><br><span class="line">?&gt;</span><br><span class="line">https:<span class="comment">//www.php.net/manual/zh/migration56.new-features.php</span></span><br></pre></td></tr></table></figure>
<h3 id="helper函数"><a href="#helper函数" class="headerlink" title="helper函数"></a>helper函数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    foreach (glob(app_path(<span class="string">'Helpers'</span>) . <span class="string">'/*.php'</span>) <span class="keyword">as</span> $file) &#123;</span><br><span class="line">        require_once $file;</span><br><span class="line">    &#125;</span><br><span class="line">     $<span class="keyword">this</span>-&gt;directoryToArray(app_path(<span class="string">'Helpers'</span>));</span><br><span class="line">&#125;</span><br><span class="line">private <span class="function"><span class="keyword">function</span> <span class="title">directoryToArray</span>(<span class="params">$directory, $recursive = false</span>) </span>&#123;</span><br><span class="line">  $array_items = array();</span><br><span class="line">  <span class="keyword">if</span>(!is_dir($directory)) <span class="keyword">return</span> <span class="string">"$directory folder does not exist"</span>;</span><br><span class="line">      <span class="keyword">if</span> ($handle = opendir($directory)) &#123;</span><br><span class="line">          <span class="keyword">while</span> (<span class="literal">false</span> !== ($file = readdir($handle))) &#123;</span><br><span class="line">              <span class="keyword">if</span> ($file != <span class="string">"."</span> &amp;&amp; $file != <span class="string">".."</span>) &#123;</span><br><span class="line">                  <span class="keyword">if</span> (is_dir($directory. <span class="string">"/"</span> . $file)) &#123;</span><br><span class="line">                      <span class="keyword">if</span>($recursive) &#123;</span><br><span class="line">                          self::directoryToArray($directory. <span class="string">"/"</span> . $file, $recursive);</span><br><span class="line">                      &#125;</span><br><span class="line">                      <span class="keyword">if</span>($<span class="keyword">this</span>-&gt;validateExt($file))&#123;</span><br><span class="line">                          $file = $directory . <span class="string">"/"</span> . $file;</span><br><span class="line">                          require_once $file;</span><br><span class="line">                      &#125;</span><br><span class="line">              &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                     <span class="keyword">if</span>($<span class="keyword">this</span>-&gt;validateExt($file))&#123;</span><br><span class="line">                          $file = $directory . <span class="string">"/"</span> . $file;</span><br><span class="line">                          require_once $file;</span><br><span class="line">                      &#125;</span><br><span class="line">              &#125;</span><br><span class="line">         &#125;</span><br><span class="line">     &#125;</span><br><span class="line">     closedir($handle);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">private <span class="function"><span class="keyword">function</span> <span class="title">validateExt</span>(<span class="params">$file</span>)</span>&#123;</span><br><span class="line">  $file_path = pathinfo($file);</span><br><span class="line">  <span class="keyword">if</span>($file_path [<span class="string">'extension'</span>] == <span class="string">'php'</span>)&#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">  &#125;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line">config/app.php</span><br><span class="line">App\Providers\HelperServiceProvider::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">App\Providers\AppServiceProvider::class,</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">carbon</span>(<span class="params">$time = null, $tz = null</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> \Carbon\Carbon($time, $tz);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="魔术方法-call"><a href="#魔术方法-call" class="headerlink" title="魔术方法__call"></a>魔术方法__call</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Family</span></span>&#123;</span><br><span class="line">            protected $childName = [];</span><br><span class="line">            public <span class="function"><span class="keyword">function</span> <span class="title">fatherName</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="string">'Dale'</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            public <span class="function"><span class="keyword">function</span> <span class="title">motherName</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="string">'Kelly'</span>;                </span><br><span class="line">            &#125;</span><br><span class="line">            public <span class="function"><span class="keyword">function</span> <span class="title">__call</span>(<span class="params">$name,$param</span>)</span>&#123;</span><br><span class="line">                <span class="keyword">if</span>($name == <span class="string">'setChildName'</span>)&#123;</span><br><span class="line">                        $childNmae = [$param];</span><br><span class="line">                        <span class="keyword">return</span> $<span class="keyword">this</span> </span><br><span class="line">                    &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">     &#125;</span><br><span class="line">     $family = <span class="keyword">new</span> Family();</span><br><span class="line">     $setChildName = $family-&gt;setChildName(<span class="string">'Kobe'</span>);</span><br><span class="line">在使用服务容器时，我们的目标是以下面形式调用我的方法。</span><br><span class="line"></span><br><span class="line">$myContainer = app(<span class="string">'myContainer'</span>);</span><br><span class="line">$myContainer-&gt;myClass()-&gt;myFunction();</span><br><span class="line">当然我可以用构造函数的方法来解决，但是当依赖了很多类的时候，构造函数是毁灭性的。调用一个方法会实例化N个类，十分不可取。所以我改造myClass如下。</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">myClass</span></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__call</span>(<span class="params">$name, $arguments</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $className = <span class="string">'App\Container\\'</span>.$name;</span><br><span class="line">        <span class="keyword">if</span> (class_exists($className))&#123;</span><br><span class="line">            $<span class="class"><span class="keyword">class</span> </span>= <span class="keyword">new</span> \ReflectionClass($className);</span><br><span class="line">            <span class="keyword">return</span> $<span class="class"><span class="keyword">class</span>-&gt;<span class="title">newInstanceArgs</span>($<span class="title">arguments</span>)</span>;</span><br><span class="line">        &#125;    </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">这样会在调用的时候，只会实例化一个类，而且代码简洁明了https:<span class="comment">//dalebao.github.io/2019/04/27/Laravel-and-magic/</span></span><br></pre></td></tr></table></figure>
<h3 id="获取每年所有周"><a href="#获取每年所有周" class="headerlink" title="获取每年所有周"></a>获取每年所有周</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">get_week</span>(<span class="params">$year</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $year_start = $year . <span class="string">"-01-01"</span>;</span><br><span class="line">    $year_end = $year . <span class="string">"-12-31"</span>;</span><br><span class="line">    $startday = strtotime($year_start);</span><br><span class="line">    <span class="keyword">if</span> (intval(date(<span class="string">'N'</span>, $startday)) != <span class="string">'1'</span>) &#123;</span><br><span class="line">        $startday = strtotime(<span class="string">"next monday"</span>, strtotime($year_start)); <span class="comment">//获取年第一周的日期</span></span><br><span class="line">    &#125;</span><br><span class="line">    $year_mondy = date(<span class="string">"Y-m-d"</span>, $startday); <span class="comment">//获取年第一周的日期</span></span><br><span class="line"></span><br><span class="line">    $endday = strtotime($year_end);</span><br><span class="line">    <span class="keyword">if</span> (intval(date(<span class="string">'W'</span>, $endday)) == <span class="string">'7'</span>) &#123;</span><br><span class="line">        $endday = strtotime(<span class="string">"last sunday"</span>, strtotime($year_end));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//如果是当前年则统计到当前周</span></span><br><span class="line">    $now_year = date(<span class="string">'Y'</span>);</span><br><span class="line">    <span class="keyword">if</span> ($now_year == $year) &#123;</span><br><span class="line">        $num = date(<span class="string">'W'</span>, strtotime(date(<span class="string">'Y-m-d'</span>)));</span><br><span class="line">    &#125;  <span class="keyword">else</span> &#123;</span><br><span class="line">        $num = intval(date(<span class="string">'W'</span>, $endday));</span><br><span class="line">        <span class="keyword">if</span> ($num == <span class="string">'1'</span>)&#123;</span><br><span class="line">            $num = intval(date(<span class="string">'W'</span>,$endday -(<span class="number">7</span>*<span class="number">24</span>*<span class="number">3600</span>)))+<span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> ($i = <span class="number">1</span>; $i &lt;= $num; $i++) &#123;</span><br><span class="line">        $j = $i <span class="number">-1</span>;</span><br><span class="line">        $start_date = date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"$year_mondy $j week "</span>));</span><br><span class="line">        $end_day = date(<span class="string">"Y-m-d"</span>, strtotime(<span class="string">"$start_date +6 day"</span>));</span><br><span class="line">        <span class="keyword">if</span> (!(date(<span class="string">"Y"</span>,strtotime(<span class="string">"$year_mondy $j week "</span>)) &gt; $year))&#123;</span><br><span class="line">            $week_array[$i] = array(</span><br><span class="line">                substr(str_replace(<span class="string">"-"</span>, <span class="string">"."</span>, $start_date), <span class="number">5</span>),</span><br><span class="line">                substr(str_replace(<span class="string">"-"</span>, <span class="string">"."</span>, $end_day), <span class="number">5</span>));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $week_array;</span><br><span class="line">&#125;https:<span class="comment">//dalebao.github.io/2019/04/27/%E8%8E%B7%E5%8F%96%E6%AF%8F%E5%B9%B4%E6%89%80%E6%9C%89%E5%91%A8/</span></span><br></pre></td></tr></table></figure>
<h3 id="php-yield"><a href="#php-yield" class="headerlink" title="php yield"></a>php yield</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"> public <span class="function"><span class="keyword">function</span> <span class="title">parseStr</span>(<span class="params">...$param</span>)</span>&#123;</span><br><span class="line">        $str = <span class="string">''</span>;</span><br><span class="line">        foreach ($param <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">            <span class="comment">//处理数组</span></span><br><span class="line">            <span class="keyword">if</span> (is_array($value)) &#123;</span><br><span class="line">                $value = md5(json_encode($value));</span><br><span class="line">            &#125;</span><br><span class="line">            $str .= $value;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $str;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 单独输出到updateChapter.log文件</span></span><br><span class="line">        \Log::useFiles(storage_path().<span class="string">'/logs/data.log'</span>);</span><br><span class="line">        $iTime = time();</span><br><span class="line">        \Log::info(<span class="string">'start'</span>, [<span class="string">'timestamp'</span> =&gt; $iTime]);</span><br><span class="line">        $i = <span class="number">0</span>;</span><br><span class="line">        $ret =  $<span class="keyword">this</span>-&gt;traverse();</span><br><span class="line">        foreach ($ret <span class="keyword">as</span> $v) &#123;</span><br><span class="line">            foreach ($v <span class="keyword">as</span> $value) &#123;</span><br><span class="line">                $key = $<span class="keyword">this</span>-&gt;parseStr(<span class="string">'wx:'</span>, $value-&gt;wx_id);</span><br><span class="line">                $count = (int) Redis::pfcount($key);</span><br><span class="line">                <span class="comment">// redis里的uv更大时更新，并记录更新前后数据</span></span><br><span class="line">                <span class="keyword">if</span> ($count &gt; $value-&gt;uv) &#123;</span><br><span class="line">                    \Log::info(<span class="string">'update id:'</span>.$value-&gt;id, [$value-&gt;<span class="function"><span class="params">uv</span> =&gt;</span> $count]);</span><br><span class="line">                    $i++;</span><br><span class="line">                    (<span class="keyword">new</span> StatisticModel([<span class="string">'hash_id'</span> =&gt; $value-&gt;wx_id]))-&gt;where(<span class="string">'id'</span>, $value-&gt;id)-&gt;update([<span class="string">'uv'</span> =&gt; $count, <span class="string">'update_time'</span> =&gt; $iTime]);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 记录更新时间与数量</span></span><br><span class="line">        \Log::info(<span class="string">'end'</span>, [<span class="string">'timestamp'</span>=&gt;time(), <span class="string">'count'</span> =&gt; $i,<span class="string">'interval'</span> =&gt; time() - $iTime]);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">traverse</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $page = <span class="number">3000</span>;</span><br><span class="line">        <span class="keyword">for</span> ($i = <span class="number">1</span>; $i &lt; <span class="number">6</span>; $i++) &#123;</span><br><span class="line">            $count = DB::table(<span class="string">'wx_statistics_'</span>.$i)-&gt;count();</span><br><span class="line">            <span class="keyword">if</span> ($count &lt; <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="comment">// 记录进度</span></span><br><span class="line">                \Log::info(<span class="string">'update schedule'</span>, [<span class="string">'table success'</span> =&gt; $i]);</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            $num = ceil($count/$page);</span><br><span class="line">            <span class="keyword">for</span> ($j = <span class="number">0</span>; $j &lt; $num; $j++) &#123;</span><br><span class="line">                $ret = DB::table(<span class="string">'wx_statistics_'</span>.$i)</span><br><span class="line">                    -&gt;select([<span class="string">'id'</span>, <span class="string">'wx_id'</span>, <span class="string">'uv'</span>])</span><br><span class="line">                    -&gt;orderBy(<span class="string">'id'</span>, <span class="string">'asc'</span>)</span><br><span class="line">                    -&gt;skip($j*$page)</span><br><span class="line">                    -&gt;take($page)</span><br><span class="line">                    -&gt;get()</span><br><span class="line">                    -&gt;toArray();</span><br><span class="line"></span><br><span class="line">                <span class="keyword">if</span> (empty($ret)) &#123;</span><br><span class="line">                    <span class="keyword">continue</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">yield</span> $ret;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            \Log::info(<span class="string">'update schedule'</span>, [<span class="string">'table success'</span> =&gt; $i]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">http:<span class="comment">//blog.13sai.com/essay/199</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-规范-Symfony-代码规范"><a href="#PHP-规范-Symfony-代码规范" class="headerlink" title="PHP 规范 - Symfony 代码规范"></a>PHP 规范 - Symfony 代码规范</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">使用 php-cs-fixer 工具来自动检查编码规范</span><br><span class="line"></span><br><span class="line">$ cd your-project/</span><br><span class="line">$ php php-cs-fixer.phar fix -v</span><br><span class="line">https:<span class="comment">//learnku.com/articles/36079</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-限制条数后再分页"><a href="#Laravel-限制条数后再分页" class="headerlink" title="Laravel 限制条数后再分页"></a>Laravel 限制条数后再分页</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">$per_page = <span class="number">15</span>; <span class="comment">// 每页条数</span></span><br><span class="line">$limit_page = <span class="number">30</span>; <span class="comment">// 限制总页数</span></span><br><span class="line">$data = Article::orderBy(<span class="string">'id'</span>, <span class="string">'desc'</span>)-&gt;limit($per_page * $limit_page)-&gt;paginate($per_page);</span><br><span class="line">此种写法并不能生效，因为 paginate 方法中内置的 limit 会替换掉我们的 limit</span><br><span class="line">取出要展示的条数的最小 id</span><br><span class="line"></span><br><span class="line">在总记录中设置 id 大于上一条件得到的最小 id</span><br><span class="line"></span><br><span class="line">即，用 where 替换 limit 限制总条数</span><br><span class="line"></span><br><span class="line">$last_id = Article::orderBy(<span class="string">'id'</span>, <span class="string">'desc'</span>)</span><br><span class="line">    -&gt;limit($per_page * $limit_page)</span><br><span class="line">    -&gt;pluck(<span class="string">'id'</span>)</span><br><span class="line">    -&gt;sort()</span><br><span class="line">    -&gt;first();</span><br><span class="line">$data = Article::orderBy(<span class="string">'id'</span>, <span class="string">'desc'</span>)</span><br><span class="line">    -&gt;where(<span class="string">'id'</span>, <span class="string">'&gt;'</span>, $last_id)</span><br><span class="line">    -&gt;paginate($per_page);</span><br><span class="line">https:<span class="comment">//learnku.com/articles/36260</span></span><br></pre></td></tr></table></figure>
<h3 id="php-ini-配置"><a href="#php-ini-配置" class="headerlink" title="php.ini 配置"></a>php.ini 配置</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">文件上传</span><br><span class="line">file_uploads = On</span><br><span class="line">max_file_uploads = <span class="number">20</span></span><br><span class="line">upload_max_filesize = <span class="number">2</span>M</span><br><span class="line">max_execution_time = <span class="number">30</span> 值 为 <span class="number">0</span> 代表没有限制</span><br></pre></td></tr></table></figure>
<h3 id="PHP-Traits"><a href="#PHP-Traits" class="headerlink" title="PHP Traits"></a>PHP Traits</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">“ Trait 是单继承语言（如 PHP ）中的一种代码复用机制。 Trai 的目的是减少单继承语言的一些限制，能让开发者自由的重用在不同的类层次结构下几个独立类中的方法。 Trait 的语义组合与类的定义在某种程度上减少了代码的复杂度，避免了与多继承和 Mixins 相关的一些典型问题。</span><br><span class="line"></span><br><span class="line">Trait 与类非常相似，但它的目的仅仅是用更好、一致的方式汇聚一些方法。 Trait 本身不能被实例化。除了传统继承之外，它能水平组合行为，换言之，类的应用程序不需要继承。”</span><br><span class="line">namespace App\Http\Traits;</span><br><span class="line"></span><br><span class="line">use App\Brand;</span><br><span class="line">trait BrandsTrait &#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">brandsAll</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">// 从品牌表中获取所有品牌.</span></span><br><span class="line">        $brands = Brand::all();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $brands;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">use App\Http\Traits\BrandsTrait;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">YourController</span> <span class="keyword">extends</span> <span class="title">Controller</span> </span>&#123;</span><br><span class="line">        use BrandsTrait;</span><br><span class="line"></span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">addProduct</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            $brands = $<span class="keyword">this</span>-&gt;brandsAll();</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/36211</span></span><br></pre></td></tr></table></figure>
<h3 id="正则表达式"><a href="#正则表达式" class="headerlink" title="正则表达式"></a>正则表达式</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">给定一组手机号，必须由[0123456789]组成的，才选出来从哪找?从字符串的开始找，找到字符串的结束 ^ $ </span></span><br><span class="line"><span class="comment">找谁[01235689]</span></span><br><span class="line"><span class="comment">找几个?11个</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">$arr = array(<span class="string">'13800138000'</span>，<span class="string">'13487656887'</span>，<span class="string">'434456'</span>，<span class="string">'45454353434543'</span>);</span><br><span class="line"><span class="comment">//$patt = '/^[^47]&#123;11&#125;$/'; //补集方法</span></span><br><span class="line">$patt = <span class="string">'/^[01235689]&#123;11&#125;$/'</span>;<span class="comment">//集合方式</span></span><br><span class="line">foreach($arr <span class="keyword">as</span> $v)&#123;</span><br><span class="line">    preg_match_all($patt，$v，$res);</span><br><span class="line">    print_r($res);</span><br><span class="line">&#125;</span><br><span class="line"> $str = <span class="string">'tommorw is another day，o2o ，you dont bird me i dont bird you'</span>;</span><br><span class="line"></span><br><span class="line">&gt;&gt;&gt; preg_split(<span class="string">'#\W+#'</span>,$str)</span><br><span class="line">=&gt; [</span><br><span class="line">     <span class="string">"tommorw"</span>,</span><br><span class="line">     <span class="string">"is"</span>,</span><br><span class="line">     <span class="string">"another"</span>,</span><br><span class="line">     <span class="string">"day"</span>,</span><br><span class="line">     <span class="string">"o2o"</span>,</span><br><span class="line">     <span class="string">"you"</span>,</span><br><span class="line">     <span class="string">"dont"</span>,</span><br><span class="line">     <span class="string">"bird"</span>,</span><br><span class="line">     <span class="string">"me"</span>,</span><br><span class="line">     <span class="string">"i"</span>,</span><br><span class="line">     <span class="string">"dont"</span>,</span><br><span class="line">     <span class="string">"bird"</span>,</span><br><span class="line">     <span class="string">"you"</span>,</span><br><span class="line">   ]</span><br><span class="line"><span class="comment">//查询纯数字或者纯字母的词</span></span><br><span class="line">$str = <span class="string">'hello o2o 2b9 250'</span>;</span><br><span class="line">$patt = <span class="string">'/\b[a-zA-Z]+\b|\b[0-9]+\b/'</span>;<span class="comment">//最少一个</span></span><br><span class="line">preg_match_all($patt，$str，$res);</span><br><span class="line">print_r($res);</span><br><span class="line"></span><br><span class="line"><span class="comment">//查询苹果系统的产品</span></span><br><span class="line">$str = <span class="string">'ipad，iphone，imac，ipod，iamsorry'</span>;</span><br><span class="line">$patt = <span class="string">'/\bi(pad|phone|mac|pod)\b/'</span>;</span><br><span class="line">preg_match_all($patt，$str，$res);</span><br><span class="line">print_r($res);</span><br><span class="line">把手机号中间的 <span class="number">4</span> 位替换为 *</span><br><span class="line"></span><br><span class="line">$str = <span class="string">'13800138000 , 13426060134 '</span>;</span><br><span class="line"><span class="comment">//前3位和后4位放子表达式中，中间4位随便，保留子表达式.替换中间的4位</span></span><br><span class="line">$patt = <span class="string">'/(\d&#123;3&#125;)\d&#123;4&#125;(\d&#123;4&#125;)/'</span>;</span><br><span class="line"><span class="comment">//preg_match_all($patt,$str, $res);</span></span><br><span class="line"><span class="comment">//print_r($res);</span></span><br><span class="line">echo preg_replace($patt， <span class="string">'\1****\2'</span>， $str);<span class="comment">//138****8000 , 134****0134</span></span><br><span class="line">$str = <span class="string">'bob李'</span>;</span><br><span class="line">$patt = <span class="string">'/^[\x&#123;4e00&#125;-\x&#123;9fa5&#125;]+$/u'</span>;</span><br><span class="line">echo preg_match($patt，$str)?<span class="string">'国货'</span>:<span class="string">'杂货'</span>;https:<span class="comment">//learnku.com/articles/36227</span></span><br></pre></td></tr></table></figure>
<h3 id="curl-下载文件"><a href="#curl-下载文件" class="headerlink" title="curl 下载文件"></a>curl 下载文件</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">curl_download_file</span>(<span class="params">$url,$path</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $ch=curl_init();</span><br><span class="line">    curl_setopt($ch,CURLOPT_POST,<span class="number">0</span>);</span><br><span class="line">    curl_setopt($ch,CURLOPT_URL,$url);</span><br><span class="line">    curl_setopt($ch,CURLOPT_RETURNTRANSFER,<span class="number">1</span>);</span><br><span class="line">    $content=curl_exec($ch);</span><br><span class="line"><span class="comment">//    file_put_contents($path, $content);</span></span><br><span class="line"><span class="comment">//    $file=fopen($path,'w');</span></span><br><span class="line"><span class="comment">//    fwrite($file,$content);</span></span><br><span class="line"><span class="comment">//    fclose($file);</span></span><br><span class="line">    header(<span class="string">'Expires: 0'</span>); <span class="comment">// no cache</span></span><br><span class="line">    header(<span class="string">'Cache-Control: must-revalidate, post-check=0, pre-check=0'</span>);</span><br><span class="line">    header(<span class="string">'Last-Modified: '</span> . gmdate(<span class="string">'D, d M Y H:i:s'</span>, time()) . <span class="string">' GMT'</span>);</span><br><span class="line">    header(<span class="string">'Cache-Control: private'</span>, <span class="literal">false</span>);</span><br><span class="line">    header(<span class="string">'Content-Type: application/force-download'</span>);</span><br><span class="line">    header(<span class="string">'Content-Disposition: attachment; filename="'</span> . basename($url) . <span class="string">'"'</span>);</span><br><span class="line">    header(<span class="string">'Content-Transfer-Encoding: binary'</span>);</span><br><span class="line">    header(<span class="string">'Content-Length: '</span> . strlen($content)); <span class="comment">// provide file size</span></span><br><span class="line">    header(<span class="string">'Connection: close'</span>);</span><br><span class="line">    echo $content;</span><br><span class="line">&#125;</span><br><span class="line">curl_download_file(<span class="string">'https://www1.hkexnews.hk/listedco/listconews/sehk/2019/1108/2019110800352_c.xlsx'</span>,<span class="string">'test.xlsx'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="更新数据后返回原来数据"><a href="#更新数据后返回原来数据" class="headerlink" title="更新数据后返回原来数据"></a>更新数据后返回原来数据</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"> $article = Finance::where(<span class="string">'state'</span>, <span class="number">0</span>)</span><br><span class="line">                            -&gt;orderBy(<span class="string">'id'</span>, <span class="string">'asc'</span>)-&gt;select(<span class="string">'id'</span>, <span class="string">'state'</span>, <span class="string">'author'</span>)</span><br><span class="line">                            -&gt;first();</span><br><span class="line"></span><br><span class="line">        $old_article = $article-&gt;toArray();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>($article)&#123;</span><br><span class="line">            $article-&gt;state = <span class="number">1</span>;</span><br><span class="line">            $article-&gt;author = <span class="string">'elesos'</span><span class="string">';</span></span><br><span class="line"><span class="string">            $article-&gt;save();</span></span><br><span class="line"><span class="string">        &#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">        return $old_article;</span></span><br><span class="line"><span class="string">当把一个对象已经创建的实例赋给一个新变量时，新变量会访问同一个实例，就和用该对象赋值一样。此行为和给函数传递入实例时一样。可以用克隆给一个已创建的对象建立一个新实例。 </span></span><br><span class="line"><span class="string"> $old_article = $article = Finance::where('</span>state<span class="string">', 0)</span></span><br><span class="line"><span class="string">                            -&gt;orderBy('</span>id<span class="string">', '</span>asc<span class="string">')</span></span><br><span class="line"><span class="string">                            -&gt;first();</span></span><br><span class="line"><span class="string">old_article 和 article 都是指向同一块内存（或者说同一个对象实例。从 C 语言的角度理解，可以类比为他们都是保存指向同一个对象的 内存地址，即指针），那么对于 article 的修改自然也会反应到 old_article 上</span></span><br><span class="line"><span class="string">$article = Finance::where('</span>state<span class="string">', 0)</span></span><br><span class="line"><span class="string">                            -&gt;orderBy('</span>id<span class="string">', '</span>asc<span class="string">')</span></span><br><span class="line"><span class="string">                            -&gt;first();</span></span><br><span class="line"><span class="string">// 关键：复制 article 实例到 old_article。</span></span><br><span class="line"><span class="string">// 若支持，此时 old_article 指向的实例与 article 不一致</span></span><br><span class="line"><span class="string">$old_article = clone $article;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">$article-&gt;state = 1;</span></span><br><span class="line"><span class="string">$article-&gt;save();</span></span><br><span class="line"><span class="string">// 如果证实可以，此处 old_article state 应为 0</span></span><br><span class="line"><span class="string">echo $old_article-&gt;state;</span></span><br><span class="line"><span class="string">https://learnku.com/laravel/t/36338</span></span><br></pre></td></tr></table></figure>
<h3 id="生成器"><a href="#生成器" class="headerlink" title="生成器"></a>生成器</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line">最普通的方式就是一次性读取文件内容，然后再进行遍历。</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getLinesFromFile</span>(<span class="params">$fileName</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 打开文件</span></span><br><span class="line">    <span class="keyword">if</span> (!$fileHandle = fopen($fileName, <span class="string">'r'</span>)) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 一次读取每一行并保存</span></span><br><span class="line">    $lines = [];</span><br><span class="line">    <span class="keyword">while</span> (<span class="literal">false</span> !== $line = fgets($fileHandle)) &#123;</span><br><span class="line">        $lines[] = $line;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    fclose($fileHandle);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $lines;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$lines = getLinesFromFile(<span class="string">'test.txt'</span>);</span><br><span class="line">foreach ($lines <span class="keyword">as</span> $line) &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">当使用该函数读取大文件时，就会因为内存不足而报错。</span><br><span class="line"></span><br><span class="line">PHP Fatal error:  Allowed memory size <span class="keyword">of</span> <span class="number">134217728</span> bytes exhausted</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getLinesFromFile</span>(<span class="params">$fileName</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!$fileHandle = fopen($fileName, <span class="string">'r'</span>)) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (<span class="literal">false</span> !== $line = fgets($fileHandle)) &#123;</span><br><span class="line">        <span class="keyword">yield</span> $line;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    fclose($fileHandle);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$lines = getLinesFromFile(<span class="string">'test.txt'</span>);</span><br><span class="line">foreach ($lines <span class="keyword">as</span> $line) &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">yield</span>;</span><br><span class="line"><span class="keyword">yield</span> $value;</span><br><span class="line"><span class="keyword">yield</span> $key =&gt; $value;</span><br><span class="line">生成器看上去是函数，实际上是 Generator 类的实例。</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">simpleGenerator</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;   </span><br><span class="line">    <span class="keyword">yield</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">echo get_class(simpleGenerator())  <span class="comment">// Generator</span></span><br><span class="line">既然是对象，就可以将其赋值给变量。</span><br><span class="line"></span><br><span class="line">$gen = simpleGenerator();</span><br><span class="line">Generator 对象已经实现了 Iterator 接口</span><br><span class="line"></span><br><span class="line">$gen <span class="keyword">instanceof</span> Iterator <span class="comment">// true</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">echoLogger</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">while</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">        <span class="comment">// 接受外部的传值</span></span><br><span class="line">        $log = <span class="keyword">yield</span>;</span><br><span class="line">        echo <span class="string">'Log: '</span> . $log . <span class="string">"\n"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$logger = echoLogger();</span><br><span class="line">$logger-&gt;send(<span class="string">'Foo'</span>);  <span class="comment">// Log: foo</span></span><br><span class="line">$logger-&gt;send(<span class="string">'Bar'</span>);  <span class="comment">// Log: bar</span></span><br><span class="line">https:<span class="comment">//learnku.com/articles/36331</span></span><br></pre></td></tr></table></figure>
<h3 id="多维数组变一维"><a href="#多维数组变一维" class="headerlink" title="多维数组变一维"></a>多维数组变一维</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"> public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">flatten</span>(<span class="params">$array, $depth = INF</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $result = [];</span><br><span class="line">        foreach ($array <span class="keyword">as</span> $item) &#123;</span><br><span class="line">            $item = $item <span class="keyword">instanceof</span> Collection ? $item-&gt;all() : $item;</span><br><span class="line">            <span class="keyword">if</span> (! is_array($item)) &#123;</span><br><span class="line">                $result[] = $item;</span><br><span class="line">            &#125; elseif ($depth === <span class="number">1</span>) &#123;</span><br><span class="line">                $result = array_merge($result, array_values($item));</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                $result = array_merge($result, <span class="attr">static</span>::flatten($item, $depth - <span class="number">1</span>));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $result;</span><br><span class="line">    &#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/36348</span></span><br></pre></td></tr></table></figure>
<h3 id="phpexcel遇到内存溢出"><a href="#phpexcel遇到内存溢出" class="headerlink" title="phpexcel遇到内存溢出"></a>phpexcel遇到内存溢出</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ExportService</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    public <span class="keyword">static</span> $outPutFile = <span class="string">''</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 导出文件</span></span><br><span class="line"><span class="comment">     * @param string $fileName</span></span><br><span class="line"><span class="comment">     * @param $data</span></span><br><span class="line"><span class="comment">     * @param array $formFields</span></span><br><span class="line"><span class="comment">     * @return mixed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">exportData</span>(<span class="params">$fileName = <span class="string">''</span>, $data, $formFields = []</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $fileArr = [];</span><br><span class="line">        $tmpPath = \Yii::$app-&gt;params[<span class="string">'excelSavePath'</span>];</span><br><span class="line"></span><br><span class="line">        foreach (array_chunk($data, <span class="number">10000</span>) <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">            self::$outPutFile = <span class="string">''</span>;</span><br><span class="line">            $subject          = !empty($fileName) ? $fileName : <span class="string">'data_'</span>;</span><br><span class="line">            $subject          .= date(<span class="string">'YmdHis'</span>);</span><br><span class="line">            <span class="keyword">if</span> (empty($value) || empty($formFields)) &#123;</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            self::$outPutFile = $tmpPath . $subject . $key . <span class="string">'.csv'</span>;</span><br><span class="line">            <span class="keyword">if</span> (!file_exists(self::$outPutFile)) &#123;</span><br><span class="line">                touch(self::$outPutFile);</span><br><span class="line">            &#125;</span><br><span class="line">            $index  = array_keys($formFields);</span><br><span class="line">            $header = array_values($formFields);</span><br><span class="line">            self::outPut($header);</span><br><span class="line"></span><br><span class="line">            foreach ($value <span class="keyword">as</span> $k =&gt; $v) &#123;</span><br><span class="line">                $tmpData = [];</span><br><span class="line">                foreach ($index <span class="keyword">as</span> $item) &#123;</span><br><span class="line">                    $tmpData[] = isset($v[$item]) ? $v[$item] : <span class="string">''</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                self::outPut($tmpData);</span><br><span class="line">            &#125;</span><br><span class="line">            $fileArr[] = self::$outPutFile;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        $zipFile = $tmpPath . $fileName . date(<span class="string">'YmdHi'</span>) . <span class="string">'.zip'</span>;</span><br><span class="line">        $zipRes = self::zipFile($fileArr, $zipFile);</span><br><span class="line">        <span class="keyword">return</span> $zipRes;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 向文件写入数据</span></span><br><span class="line"><span class="comment">     * @param array $data</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">outPut</span>(<span class="params">$data = []</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (is_array($data) &amp;&amp; !empty($data)) &#123;</span><br><span class="line">            $data = implode(<span class="string">','</span>, $data);</span><br><span class="line">            file_put_contents(self::$outPutFile, iconv(<span class="string">"UTF-8"</span>, <span class="string">"GB2312//IGNORE"</span>, $data) . PHP_EOL, FILE_APPEND);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 压缩文件</span></span><br><span class="line"><span class="comment">     * @param $sourceFile</span></span><br><span class="line"><span class="comment">     * @param $distFile</span></span><br><span class="line"><span class="comment">     * @return mixed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">zipFile</span>(<span class="params">$sourceFile, $distFile</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $zip = <span class="keyword">new</span> \ZipArchive();</span><br><span class="line">        <span class="keyword">if</span> ($zip-&gt;open($distFile, \ZipArchive::CREATE) !== <span class="literal">true</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> $sourceFile;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $zip-&gt;open($distFile, \ZipArchive::CREATE);</span><br><span class="line">        foreach ($sourceFile <span class="keyword">as</span> $file) &#123;</span><br><span class="line">            $fileContent = file_get_contents($file);</span><br><span class="line">            $file        = iconv(<span class="string">'utf-8'</span>, <span class="string">'GBK'</span>, basename($file));</span><br><span class="line">            $zip-&gt;addFromString($file, $fileContent);</span><br><span class="line">        &#125;</span><br><span class="line">        $zip-&gt;close();</span><br><span class="line">        <span class="keyword">return</span> $distFile;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 下载文件</span></span><br><span class="line"><span class="comment">     * @param $filePath</span></span><br><span class="line"><span class="comment">     * @param $fileName</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">download</span>(<span class="params">$filePath, $fileName</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (!file_exists($filePath . $fileName)) &#123;</span><br><span class="line">            header(<span class="string">'HTTP/1.1 404 NOT FOUND'</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">//以只读和二进制模式打开文件</span></span><br><span class="line">            $file = fopen($filePath . $fileName, <span class="string">"rb"</span>);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//告诉浏览器这是一个文件流格式的文件</span></span><br><span class="line">            Header(<span class="string">"Content-type: application/octet-stream"</span>);</span><br><span class="line">            <span class="comment">//请求范围的度量单位</span></span><br><span class="line">            Header(<span class="string">"Accept-Ranges: bytes"</span>);</span><br><span class="line">            <span class="comment">//Content-Length是指定包含于请求或响应中数据的字节长度</span></span><br><span class="line">            Header(<span class="string">"Accept-Length: "</span> . filesize($filePath . $fileName));</span><br><span class="line">            <span class="comment">//用来告诉浏览器，文件是可以当做附件被下载，下载后的文件名称为$file_name该变量的值</span></span><br><span class="line">            Header(<span class="string">"Content-Disposition: attachment; filename="</span> . $fileName);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//读取文件内容并直接输出到浏览器</span></span><br><span class="line">            echo fread($file, filesize($filePath . $fileName));</span><br><span class="line">            fclose($file);</span><br><span class="line">            exit();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;		$fileName = <span class="string">"库存导入模板"</span>;</span><br><span class="line">        $stockRes = []; <span class="comment">// 导出的数据</span></span><br><span class="line">        $formFields = [</span><br><span class="line">            <span class="string">'store_id'</span>  =&gt; <span class="string">'门店ID'</span>,</span><br><span class="line">            <span class="string">'storeName'</span> =&gt; <span class="string">'门店名称'</span>,</span><br><span class="line">            <span class="string">'sku'</span>       =&gt; <span class="string">'SKU编码'</span>,</span><br><span class="line">            <span class="string">'name'</span>      =&gt; <span class="string">'SKU名称'</span>,</span><br><span class="line">            <span class="string">'stock'</span>     =&gt; <span class="string">'库存'</span>,</span><br><span class="line">            <span class="string">'reason'</span>    =&gt; <span class="string">'原因'</span></span><br><span class="line">        ];</span><br><span class="line">        $fileRes    = ExportService::exportData($fileName, $stockRes, $formFields);</span><br><span class="line">        $tmpPath    = \Yii::$app-&gt;params[<span class="string">'excelSavePath'</span>]; <span class="comment">// 文件路径</span></span><br><span class="line">        $fileName   = str_replace($tmpPath, <span class="string">''</span>, $fileRes);</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 下载文件</span></span><br><span class="line">        ExportService::download($tmpPath, $fileName);</span><br><span class="line">        https:<span class="comment">//tsmliyun.github.io/php/%E5%85%B3%E4%BA%8EPHP%E5%86%85%E5%AD%98%E6%BA%A2%E5%87%BA%E7%9A%84%E6%80%9D%E8%80%83/</span></span><br></pre></td></tr></table></figure>
<h3 id="基于雪花算法的-PHP-ID-生成器"><a href="#基于雪花算法的-PHP-ID-生成器" class="headerlink" title="基于雪花算法的 PHP ID 生成器"></a>基于雪花算法的 PHP ID 生成器</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">第一个 bit 为未使用的符号位。</span><br><span class="line">第二部分由 <span class="number">41</span> 位的时间戳（毫秒）构成，他的取值是当前时间相对于某一时间的偏移量。</span><br><span class="line">第三部分和第四部分的 <span class="number">5</span> 个 bit 位表示数据中心和机器 ID，其能表示的最大值为 <span class="number">2</span>^<span class="number">5</span> <span class="number">-1</span> = <span class="number">31</span>；</span><br><span class="line">最后部分由 <span class="number">12</span> 个 bit 组成，其表示每个工作节点每毫秒生成的序列号 ID，同一毫秒内最多可生成 <span class="number">2</span>^<span class="number">12</span> <span class="number">-1</span> 即 <span class="number">4095</span> 个 ID。</span><br><span class="line">composer <span class="built_in">require</span> godruoyi/php-snowflake</span><br><span class="line">$snowflake = <span class="keyword">new</span> \Godruoyi\Snowflake\Snowflake;</span><br><span class="line"></span><br><span class="line">$snowflake-&gt;id();</span><br><span class="line"><span class="comment">// 1537200202186752</span></span><br><span class="line">$snowflake = <span class="keyword">new</span> \Godruoyi\Snowflake\Snowflake;</span><br><span class="line">$snowflake-&gt;setStartTimeStamp(strtotime(<span class="string">'2019-09-09'</span>)*<span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line">$snowflake-&gt;id();</span><br><span class="line">use Godruoyi\Snowflake\Snowflake;</span><br><span class="line">use Godruoyi\Snowflake\LaravelSequenceResolver;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AppServiceProvider</span> <span class="keyword">extends</span> <span class="title">ServiceProvider</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Register any application services.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;app-&gt;singleton(<span class="string">'snowflake'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> (<span class="keyword">new</span> Snowflake())</span><br><span class="line">                -&gt;setStartTimeStamp(strtotime(<span class="string">'2019-08-08'</span>)*<span class="number">1000</span>)</span><br><span class="line">                -&gt;setSequenceResolver(<span class="keyword">new</span> LaravelSequenceResolver(</span><br><span class="line">                    $<span class="keyword">this</span>-&gt;app-&gt;get(<span class="string">'cache'</span>)-&gt;store()</span><br><span class="line">                ));</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">64</span> 位的二进制最大能生成 <span class="number">19</span> 位的 ID，所以在设计数据库长度的时候最好预留下长度</span><br><span class="line">  public <span class="function"><span class="keyword">function</span> <span class="title">getCurrentMicrotime</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">      </span>&#123;</span><br><span class="line">          <span class="keyword">return</span> floor(microtime(<span class="literal">true</span>) * <span class="number">1000</span>) | <span class="number">0</span>;<span class="comment">//浮点数转数字 </span></span><br><span class="line">      &#125;</span><br><span class="line"> bindec(<span class="string">'0111111111111111111111111111111111111111111111111111111111111111'</span>)</span><br><span class="line">=&gt; <span class="number">9223372036854775807</span></span><br><span class="line">&gt;&gt;&gt; strlen(<span class="number">9223372036854775807</span>)</span><br><span class="line">=&gt; <span class="number">19</span></span><br><span class="line">https:<span class="comment">//learnku.com/articles/32575</span></span><br></pre></td></tr></table></figure>
<h3 id="Nginx-与-PHP-的通信"><a href="#Nginx-与-PHP-的通信" class="headerlink" title="Nginx 与 PHP 的通信"></a>Nginx 与 PHP 的通信</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"> Nginx 并不是直接和 PHP 进行通信的，而是通过 PHP-FPM。Nginx 不仅仅是一个强大的 Web 服务器，也是一个强大的代理服务器，提供了很多请求协议的代理。比如 Http 协议还有 FastCgi 协议等。</span><br><span class="line"></span><br><span class="line">当请求进入到 Nginx 中，Nginx 提供了一个 FastCgi 模块 来把 Http 请求映射为对应的 Fastcgi 请求。该模块提供了 fastcgi_param 指定来完成映射关系。它的主要作用就是把 Nginx 中的变量翻译成 PHP 中能够理解的变量</span><br><span class="line"></span><br><span class="line">fastcgi_pass, 用来指定 FPM 进程监听的地址，Nginx 会把所有的 PHP 请求映射成 fastcgi 请求，然后发送到这个地址上。</span><br><span class="line"></span><br><span class="line">所有的 .php 结尾的请求都交给 fastcgi 模块处理，然后把处理后的请求发送给 PHP-FPM，然后 PHP-FPM 把请求交给 worker 进程，worker 进程加载 PHP 解析器运行 PHP 处理结果。 其中 fastcgi_pass unix:<span class="regexp">/var/</span>run/php/php7<span class="number">.1</span>-fpm.sock; 这一行用来指定 fpm 的地址。</span><br><span class="line">Nginx 和 PHP 的通信流程大概如下:</span><br><span class="line"></span><br><span class="line">客户端发送请求到 Nginx</span><br><span class="line">加载 nginx.conf 文件，把所有 .php 结尾的请求特殊处理</span><br><span class="line">加载 FastCGI 模块，完成请求参数的解析映射，生成 FastCGI 请求</span><br><span class="line">然后通过 fastcgi_pass 参数把 FastCGI 请求发送给 PHP-FPM 处理</span><br><span class="line">PHP-FPM 收到请求，分配给空闲 worker 子进程</span><br><span class="line">worker 子进程加载 PHP 解析器等 完成 PHP 执行获取结果</span><br><span class="line">PHP-FPM 是一种 master/worker 进程架构。首先会启动一个 master 主进程，主要功能用来完成 PHP 环境的初始化，事件监听，子进程状态管理等等。然后会启动若干 worker 子进程来处理 PHP 请求。</span><br><span class="line"></span><br><span class="line">master 进程的工作流程</span><br><span class="line"></span><br><span class="line"><span class="number">1.</span> 初始化 CGI，注册进程信号初始化全局变量。</span><br><span class="line"><span class="number">2.</span> 完成 PHP 环境初始化。加载 php.ini 解析配置文件，加载 PHP 模块记录函数符号表，加载 zend 扩展，设置禁用函数和类库设置，注册回收内存方法。</span><br><span class="line"><span class="number">3.</span> 完成 PHP-FPM 初始化。加载并解析 php-fpm.conf 文件，获取进程相关参数，初始化进程池以及事件模型等。</span><br><span class="line"><span class="number">4.</span> 处理子进程相关操作。fork 子进程，进行事件监听等。</span><br><span class="line"></span><br><span class="line">worker 进程工作流程</span><br><span class="line"></span><br><span class="line"><span class="number">1.</span> 接收请求。这里是不需要初始化 PHP 运行环境的。</span><br><span class="line"><span class="number">2.</span> 处理请求。获取请求内容注册全局变量 ($_GET,$_POST,$_SERVER 等)，然后根据请求信息访问对应的 PHP 文件，然后将 PHP 脚本文件交给 Zend 引擎处理。</span><br><span class="line"><span class="number">3.</span> 处理结果。在 Zend 引擎处理完毕后将会进行回调，发送响应内容，释放内存等</span><br><span class="line"></span><br><span class="line">测试 php-fpm 配置内容是否正确 使用 -t 参数， 还可以通过加 -c 指定 php.ini 文件，通过 -y 指定 php-fpm.conf 文件 </span><br><span class="line"></span><br><span class="line">/usr/sbin/php-fpm7 -t</span><br><span class="line">/usr/sbin/php-fpm7 -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf -t</span><br><span class="line"></span><br><span class="line">使用 root 权限启动子进程 通过增加 -R 参数</span><br><span class="line"></span><br><span class="line">/usr/sbin/php-fpm7 -c xxx/xxx/xxx/php.ini -y /xxx/xxx/xxx/php-fpm.conf -R</span><br><span class="line"></span><br><span class="line">重新创建容器 并添加 --cap-add=SYS_PTRACE 给容器追加 Ptrace 功能</span><br><span class="line"></span><br><span class="line">docker run --name website_name -p <span class="number">11280</span>:<span class="number">80</span> --cap-add=SYS_PTRACE -v /data/website/website_name:<span class="regexp">/app -d showtime/</span>php-javabridge:v1  https:<span class="comment">//learnku.com/articles/28683</span></span><br></pre></td></tr></table></figure>
<h3 id="Elasticsearch-Algolia-全文搜索"><a href="#Elasticsearch-Algolia-全文搜索" class="headerlink" title="Elasticsearch/Algolia 全文搜索"></a>Elasticsearch/Algolia 全文搜索</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//www.elastic.co/cn/downloads/elasticsearch</span></span><br><span class="line">新版的 ES 是内置的 java 环境</span><br><span class="line">windows 直接启动 elasticsearch.bat </span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/articles/30812#reply117421</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-系统调用time"><a href="#PHP-系统调用time" class="headerlink" title="PHP 系统调用time()"></a>PHP 系统调用time()</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">$year = date(<span class="string">'Y'</span>, time());</span><br><span class="line">$month = date(<span class="string">'m'</span>, time());</span><br><span class="line">系统调用就是操作系统提供给用户程序访问计算机资源的接口，更要命的是，系统调用极其耗时</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TimeWrapper</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    private <span class="keyword">static</span> $now_time = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @param bool $force_refresh 是否强制刷新</span></span><br><span class="line"><span class="comment">     * @return int</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getTime</span>(<span class="params">$force_refresh = false</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($force_refresh) &#123;</span><br><span class="line">            self::$now_time = time();</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (!self::$now_time) &#123;</span><br><span class="line">                self::$now_time = time();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> self::$now_time;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/36538</span></span><br></pre></td></tr></table></figure>
<h3 id="curl-下载进度"><a href="#curl-下载进度" class="headerlink" title="curl 下载进度"></a>curl 下载进度</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Downloader</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> $lastDownloaded = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">static</span> $lastTime = <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">	public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">download</span>(<span class="params">$url, $fileName, $date</span>)</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		ini_set(<span class="string">'memory_limit'</span>, <span class="attr">Config</span>::$memory_limit);	<span class="comment">//调整最大占用内存</span></span><br><span class="line">		$code = [<span class="string">'"'</span>, <span class="string">'*'</span>, <span class="string">':'</span>, <span class="string">'&lt;'</span>, <span class="string">'&gt;'</span>, <span class="string">'？'</span>, <span class="string">'/'</span>, <span class="string">'\\'</span>, <span class="string">'|'</span>];</span><br><span class="line">		$fileName = preg_replace(<span class="string">'# #'</span>,<span class="string">''</span>,$fileName);</span><br><span class="line">		$fileName = str_replace($code, <span class="string">''</span>, $fileName);</span><br><span class="line">		<span class="keyword">if</span> (!is_dir(Config::$path)) &#123;</span><br><span class="line">			mkdir(Config::$path);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		$filePath = Config::$path.<span class="string">'/'</span>.date(<span class="string">'Ymd'</span>,strtotime($date)).<span class="string">'_'</span>.$fileName.<span class="string">'.mp4'</span>;</span><br><span class="line">		<span class="keyword">if</span> (file_exists($filePath))&#123;</span><br><span class="line">			echo <span class="string">"\033[0;32m"</span>.<span class="string">"文件已存在"</span>.<span class="string">"\033[0m\n"</span>;</span><br><span class="line">			<span class="keyword">return</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		$header = array();</span><br><span class="line">		$header[] = <span class="string">"User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36Name'"</span>;</span><br><span class="line">		$header[] = <span class="string">"Referer:http://91porn.com"</span>;</span><br><span class="line"></span><br><span class="line">		$ch = curl_init();</span><br><span class="line">		<span class="comment">// 从配置文件中获取根路径</span></span><br><span class="line">		curl_setopt($ch, CURLOPT_URL, $url);</span><br><span class="line">		curl_setopt($ch, CURLOPT_TIMEOUT,<span class="number">300</span>);</span><br><span class="line">		curl_setopt($ch, CURLOPT_RETURNTRANSFER, <span class="literal">true</span>);</span><br><span class="line">		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);</span><br><span class="line">		<span class="keyword">if</span> (property_exists(<span class="string">'Config'</span>, <span class="string">'proxy'</span>)) &#123;</span><br><span class="line">			curl_setopt($ch, CURLOPT_PROXY, <span class="attr">Config</span>::$proxy);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">// 开启进度条</span></span><br><span class="line">		curl_setopt($ch, CURLOPT_NOPROGRESS, <span class="literal">false</span>);</span><br><span class="line">		<span class="comment">// 进度条的触发函数</span></span><br><span class="line">		curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, array(<span class="keyword">new</span> self, <span class="string">'progress'</span>));</span><br><span class="line">		<span class="comment">// ps: 如果目标网页跳转，也跟着跳转</span></span><br><span class="line">		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, <span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">		$data = curl_exec($ch);</span><br><span class="line">		curl_close($ch);</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span> ($data) &#123;</span><br><span class="line">			$file = fopen($filePath,<span class="string">"w+"</span>);</span><br><span class="line">			fputs($file,$data);<span class="comment">//写入文件</span></span><br><span class="line">			fclose($file);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">// 使用rclone上传onedrive，其中“91porn:/91porn”对应网盘名称和路径</span></span><br><span class="line">		<span class="comment">// $command = 'rclone move -P '.$filePath.' 91porn:/91porn';</span></span><br><span class="line">		<span class="comment">// system($command);</span></span><br><span class="line"></span><br><span class="line">		unset($data);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	* 进度条下载.https://github.com/zzjzz9266a/91porn_php</span></span><br><span class="line"><span class="comment">	*</span></span><br><span class="line"><span class="comment">	* @param $ch</span></span><br><span class="line"><span class="comment">	* @param $downloadSize 总下载量</span></span><br><span class="line"><span class="comment">	* @param $downloaded 当前下载量</span></span><br><span class="line"><span class="comment">	* @param $uploadSize </span></span><br><span class="line"><span class="comment">	* @param $uploaded</span></span><br><span class="line"><span class="comment">	*/</span></span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">progress</span>(<span class="params">$resource, $downloadSize = <span class="number">0</span>, $downloaded = <span class="number">0</span>, $uploadSize = <span class="number">0</span>, $uploaded = <span class="number">0</span></span>)</span>&#123;</span><br><span class="line">		<span class="keyword">if</span> ($downloadSize === <span class="number">0</span>) &#123;</span><br><span class="line">			<span class="keyword">return</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> ($downloaded == $downloadSize) &#123;</span><br><span class="line">			printf(<span class="string">"下载完成: %.1f%%, %.2f MB/%.2f MB\n"</span>, $downloaded/$downloadSize*<span class="number">100</span>, $downloaded/<span class="number">1000000</span>, $downloadSize/<span class="number">1000000</span>);</span><br><span class="line">			Downloader::$lastDownloaded = <span class="number">0</span>;</span><br><span class="line">			Downloader::$lastTime = <span class="number">0</span>;</span><br><span class="line">			<span class="keyword">return</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> (microtime(<span class="literal">true</span>)-Downloader::$lastTime &lt;= <span class="number">1</span>) &#123;</span><br><span class="line">			<span class="keyword">return</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		$speed = ($downloaded-Downloader::$lastDownloaded)/(microtime(<span class="literal">true</span>)-Downloader::$lastTime)/<span class="number">1000</span>;</span><br><span class="line"></span><br><span class="line">		Downloader::$lastDownloaded = $downloaded;</span><br><span class="line">		Downloader::$lastTime = microtime(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">		$downloaded = $downloaded/<span class="number">1000000</span>;</span><br><span class="line">		$downloadSize = $downloadSize/<span class="number">1000000</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span> ($speed &lt; <span class="number">1000</span>) &#123;</span><br><span class="line">			$speedStr = <span class="string">", 下载速度：%.2f kb/s     "</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			$speedStr = <span class="string">", 下载速度：%.2f mb/s     "</span>;</span><br><span class="line">			$speed = $speed/<span class="number">1000</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		$progress = $downloaded/$downloadSize*<span class="number">100</span>;</span><br><span class="line">		printf(<span class="string">"下载进度: %.1f%%, %.2f MB/%.2f MB"</span>.$speedStr.<span class="string">"\r"</span>, $progress, $downloaded, $downloadSize, $speed);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">random_ip</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	$a = rand(<span class="number">0</span>, <span class="number">255</span>);</span><br><span class="line">	$b = rand(<span class="number">0</span>, <span class="number">255</span>);</span><br><span class="line">	$c = rand(<span class="number">0</span>, <span class="number">255</span>);</span><br><span class="line">	$d = rand(<span class="number">0</span>, <span class="number">255</span>);</span><br><span class="line">	<span class="keyword">return</span> $a.<span class="string">'.'</span>.$b.<span class="string">'.'</span>.$c.<span class="string">'.'</span>.$d;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="队列执行频率限制"><a href="#队列执行频率限制" class="headerlink" title="队列执行频率限制"></a>队列执行频率限制</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">每分钟限制执行 <span class="number">10</span> 次 JOB（注意是 JOB，而不是整个队列）</span><br><span class="line"></span><br><span class="line">use Illuminate\Support\Facades\Redis;</span><br><span class="line">Redis::throttle(<span class="string">'key'</span>)-&gt;allow(<span class="number">10</span>)-&gt;every(<span class="number">60</span>)-&gt;then(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 任务逻辑...</span></span><br><span class="line">&#125;, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 无法获得锁...</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;release(<span class="number">10</span>);</span><br><span class="line">&#125;);</span><br><span class="line">并发，限制同一时间只执行一个 JOB</span><br><span class="line"></span><br><span class="line">Redis::funnel(<span class="string">'key'</span>)-&gt;limit(<span class="number">1</span>)-&gt;then(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 任务逻辑...</span></span><br><span class="line">&#125;, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 无法获得锁...</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;release(<span class="number">10</span>);</span><br><span class="line">&#125;);方法里的参数 key 是自定义的 redis key，如果需要多个 job 共用一个限制，则可以使用同一个 key</span><br><span class="line">   </span><br><span class="line">https:<span class="comment">//learnku.com/articles/30054</span></span><br></pre></td></tr></table></figure>
<h3 id="上传文件和下载"><a href="#上传文件和下载" class="headerlink" title="上传文件和下载"></a>上传文件和下载</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">/*</span></span><br><span class="line"><span class="comment">        文件上传函数</span></span><br><span class="line"><span class="comment">        @param  string  $name  文件上传文件域的name值</span></span><br><span class="line"><span class="comment">        @param  string  $dir   文件保存路径</span></span><br><span class="line"><span class="comment">        @param  array   $allow 文件允许上传的类型</span></span><br><span class="line"><span class="comment">        return  string  $filename 文件名  如果失败 返回false</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">upload</span>(<span class="params">$name,$dir=<span class="string">'./upload/'</span>,$allow=array(<span class="string">'jpg'</span>,<span class="string">'gif'</span>,<span class="string">'jpeg'</span>,<span class="string">'png'</span></span>))</span>&#123;</span><br><span class="line">        <span class="comment">//echo $name;exit;</span></span><br><span class="line">        <span class="comment">//var_dump($_FILES);exit;</span></span><br><span class="line">        <span class="comment">//1.判断文件上传错误</span></span><br><span class="line">        <span class="keyword">if</span>($_FILES[$name][<span class="string">'error'</span>]&gt;<span class="number">0</span>)&#123;</span><br><span class="line">            <span class="comment">//echo '上传错误';</span></span><br><span class="line">            <span class="keyword">switch</span>($_FILES[$name][<span class="string">'error'</span>])&#123;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">                    echo <span class="string">'上传的文件超过了 php.ini 中upload_max_filesize 选项限制的值.'</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">                    echo <span class="string">'上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值'</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">                    echo <span class="string">'文件只有部分被上传.'</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">                    echo <span class="string">'没有文件被上传.'</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">6</span>:</span><br><span class="line">                    echo <span class="string">'找不到临时文件夹.'</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">7</span>:</span><br><span class="line">                    echo <span class="string">'文件写入失败.'</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//2.判断你文件上传的类型是否是你想要的类型</span></span><br><span class="line">        <span class="comment">//2.1允许上传的类型</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//2.2 获取后缀名</span></span><br><span class="line">        $suffix = pathinfo($_FILES[$name][<span class="string">'name'</span>],PATHINFO_EXTENSION);</span><br><span class="line">        <span class="comment">//echo $suffix;exit;</span></span><br><span class="line">        <span class="comment">//2.3 判断是否是我们允许上传的类型</span></span><br><span class="line">        <span class="comment">//var_dump(in_array($suffix,$allow));exit;</span></span><br><span class="line">        <span class="keyword">if</span>(!in_array($suffix,$allow))&#123;</span><br><span class="line">            <span class="comment">//不允许上传的类型</span></span><br><span class="line">            echo  <span class="string">'大哥你的上传类型不符合'</span>;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//3.起名字</span></span><br><span class="line">        $filename = date(<span class="string">'Ymd'</span>).uniqid().mt_rand(<span class="number">0</span>,<span class="number">9999</span>).<span class="string">'.'</span>.$suffix;</span><br><span class="line">        <span class="comment">//echo $filename;exit;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//4.判断保存路径是否存在</span></span><br><span class="line">        <span class="comment">//4.1 得到保存路径</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//4.2 处理保存路径和后面的斜杠</span></span><br><span class="line">        $save_path = rtrim($dir,<span class="string">'/'</span>);</span><br><span class="line">        $save_path .=<span class="string">'/'</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//4.3 保存路径中的时间文件夹处理</span></span><br><span class="line">        $save_path .=date(<span class="string">'Y/m/d/'</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//4.4 判断保存的路径是否存在</span></span><br><span class="line">        <span class="keyword">if</span>(!file_exists($save_path))&#123;</span><br><span class="line">            mkdir($save_path,<span class="number">777</span>,<span class="literal">true</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//4.5 拼接一个完整的保存路径</span></span><br><span class="line">        $path = $save_path.$filename;</span><br><span class="line">        <span class="comment">//echo $path;exit;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//5.判断是否是httppost方式上传</span></span><br><span class="line">        <span class="keyword">if</span>(!is_uploaded_file($_FILES[$name][<span class="string">'tmp_name'</span>]))&#123;</span><br><span class="line">            echo <span class="string">'滚蛋！'</span>;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//6.移动图片</span></span><br><span class="line">        <span class="keyword">if</span>(!move_uploaded_file($_FILES[$name][<span class="string">'tmp_name'</span>],$path))&#123;</span><br><span class="line">            echo <span class="string">'移动失败'</span>;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//7.返回移动成功的图片名</span></span><br><span class="line">        <span class="keyword">return</span> $filename;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    下载文件</span><br><span class="line">     &lt;!-- 浏览器认识这样的类型,就会被解析 --&gt;</span><br><span class="line">        &lt;a href=<span class="string">"./action.php?name=1.html"</span>&gt;<span class="number">1.</span>html&lt;<span class="regexp">/a&gt;</span></span><br><span class="line"><span class="regexp">        &lt;a href="./</span>action.php?name=<span class="number">1.</span>php<span class="string">"&gt;1.php&lt;/a&gt;</span></span><br><span class="line"><span class="string">        &lt;a href="</span>./action.php?name=<span class="number">1.</span>txt<span class="string">"&gt;1.txt&lt;/a&gt;</span></span><br><span class="line"><span class="string">        &lt;a href="</span>./action.php?name=<span class="number">1.</span>jpg<span class="string">"&gt;1.jpg&lt;/a&gt;</span></span><br><span class="line"><span class="string">    //接收一下name值.</span></span><br><span class="line"><span class="string">    $name = $_GET['name'];</span></span><br><span class="line"><span class="string">    </span></span><br><span class="line"><span class="string">    //实现下载功能</span></span><br><span class="line"><span class="string">    //强制浏览器弹出另存为对话框</span></span><br><span class="line"><span class="string">    header('content-Disposition:attachment;filename="</span><span class="string">'.$name.'</span><span class="string">"');</span></span><br><span class="line"><span class="string">    </span></span><br><span class="line"><span class="string">    //此时只是下载了一个空文件,需要利用readfile读一遍所有的内容.便可下载.</span></span><br><span class="line"><span class="string">    $path = './downlist/'.$name;</span></span><br><span class="line"><span class="string">    readfile($path);</span></span><br><span class="line"><span class="string">https://learnku.com/articles/36744</span></span><br></pre></td></tr></table></figure>
<h3 id="app-接口对接"><a href="#app-接口对接" class="headerlink" title="app 接口对接"></a>app 接口对接</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (empty($arr)) &#123;</span><br><span class="line">   $arr = (object) <span class="literal">null</span>;<span class="comment">//$arr = new stdClass;</span></span><br><span class="line">&#125;</span><br><span class="line">echo json_encode($arr);</span><br><span class="line">$arr = [<span class="string">"id"</span>=&gt;<span class="string">"1"</span>,<span class="string">"name"</span>=&gt;<span class="literal">null</span>];</span><br><span class="line">$data = [</span><br><span class="line"> <span class="string">"id"</span>=&gt;(int) $arr[<span class="string">"id"</span>],</span><br><span class="line"><span class="string">"name"</span>=&gt;(string) $arr[<span class="string">"name"</span>],</span><br><span class="line">];</span><br><span class="line">echo json_encode($data);</span><br><span class="line">json_encode ($result ,JSON_FORCE_OBJECT)，返回格式为 &#123;&#125;,</span><br></pre></td></tr></table></figure>
<h3 id="异常类"><a href="#异常类" class="headerlink" title="异常类"></a>异常类</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">doSomething</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(something error)&#123;</span><br><span class="line">         <span class="keyword">throw</span> <span class="keyword">new</span> SomethingException(<span class="string">'something error'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">    <span class="keyword">do</span> something</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//调用</span></span><br><span class="line"><span class="keyword">try</span>&#123;</span><br><span class="line">   doSomething();</span><br><span class="line">&#125;<span class="keyword">catch</span>(SomethingException $e)&#123;</span><br><span class="line">   echo <span class="string">"something goes error"</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 调用方无法从外部得知时哪里出了问题，因为只返回了 false</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">checkoutOrder</span>(<span class="params">$orderNumber</span>)</span>&#123;</span><br><span class="line">  $orderModel = OrderModel::query()-&gt;where(<span class="string">'order_number'</span>,$orderNumber)-&gt;first();</span><br><span class="line">  <span class="keyword">if</span>(!$orderModel)&#123;</span><br><span class="line">     <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  $payResut = PayService::pay($orderModel);</span><br><span class="line">  <span class="keyword">if</span>(!$payResut)&#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">checkoutOrder</span>(<span class="params">$orderNumber</span>)</span>&#123;</span><br><span class="line">  $orderModel = OrderModel::query()-&gt;where(<span class="string">'order_number'</span>,$orderNumber)-&gt;first();</span><br><span class="line">  <span class="keyword">if</span>(!$orderModel)&#123;</span><br><span class="line">     <span class="keyword">throw</span> <span class="keyword">new</span> OrderNotFoundException($orderNumber);</span><br><span class="line">  &#125;</span><br><span class="line">  $payResut = PayService::pay($orderModel);</span><br><span class="line">  <span class="keyword">if</span>(!$payResut)&#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> PaymentException(); <span class="comment">// 这个异常应该上面的 PayService 中抛出，为了更清晰就写在这</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 调用</span></span><br><span class="line"><span class="keyword">try</span>&#123;</span><br><span class="line">    checkoutOrder(<span class="string">'ORDER00001'</span>);</span><br><span class="line">&#125;<span class="keyword">catch</span>(OrderNotFoundException $e)&#123;</span><br><span class="line">    <span class="keyword">return</span> response(<span class="string">'订单不存在：'</span>.$e-&gt;getMessage(),<span class="number">404</span>);</span><br><span class="line">&#125;<span class="keyword">catch</span>(PaymentException $e)&#123;</span><br><span class="line">    <span class="keyword">return</span> response(<span class="string">'支付失败：'</span>.$e-&gt;getMessage(),<span class="number">500</span>);</span><br><span class="line">&#125;</span><br><span class="line">app/Exceptions/Handler.php 中根据异常名、紧急程度调用第三方通知工具（钉钉、邮件等）通知项目错误。</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">report</span>(<span class="params">Exception $exception</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;shouldntReport($exception)) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 如果异常类中存在 report 方法，就使用自身的</span></span><br><span class="line">    <span class="keyword">if</span> (method_exists($exception, <span class="string">'report'</span>)) &#123;</span><br><span class="line">        <span class="keyword">return</span> $exception-&gt;report();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    $msg = <span class="string">"系统异常："</span> . $exception-&gt;getMessage();</span><br><span class="line">    $msg .= <span class="string">"\n文件："</span> . $exception-&gt;getFile();</span><br><span class="line">    $msg .= <span class="string">"\n行号："</span> . $exception-&gt;getLine();</span><br><span class="line">    $msg .= <span class="string">"\n参数:"</span> . json_encode([<span class="string">'form_params'</span> =&gt; request()-&gt;all()]);</span><br><span class="line"></span><br><span class="line">    DingService::sendWarning($msg);</span><br><span class="line">    parent::report($exception);</span><br><span class="line">&#125;</span><br><span class="line">在 render 方法根据异常名返回不同的客户端响应：</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">$request, Exception $exception</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> ($exception <span class="keyword">instanceof</span> OrderException) &#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;handleOrderException($exception, $request);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ($exception <span class="keyword">instanceof</span> PaymentException) &#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;handlePaymentException($exception, $request);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> parent::render($request, $exception);</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/36657</span></span><br></pre></td></tr></table></figure>
<h3 id="where-or-查询"><a href="#where-or-查询" class="headerlink" title="where or 查询"></a>where or 查询</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"> SELECT * FROM user</span><br><span class="line">  WHERE </span><br><span class="line">    group_id = <span class="string">'group id'</span></span><br><span class="line">   AND (</span><br><span class="line">     name = <span class="string">'name'</span></span><br><span class="line">     OR mobile_number = <span class="string">'mobile number'</span></span><br><span class="line">     OR email = <span class="string">'email'</span></span><br><span class="line">     OR <span class="string">`score`</span> &gt; <span class="number">1000</span></span><br><span class="line">   ) </span><br><span class="line">  DB::table(<span class="string">'users'</span>)</span><br><span class="line">     -&gt;where(<span class="string">'group_id'</span>, <span class="string">'group id'</span>)</span><br><span class="line">     -&gt;where(<span class="function"><span class="keyword">function</span> (<span class="params">$query</span>) </span>&#123;</span><br><span class="line">         $query-&gt;where(<span class="string">'name'</span>, <span class="string">'name'</span>)</span><br><span class="line">               -&gt;orWhere(<span class="string">'mobile_number'</span>, <span class="string">'='</span>, <span class="string">'mobile number'</span>)</span><br><span class="line">               -&gt;orWhere(<span class="string">'email'</span>, <span class="string">'='</span>, <span class="string">'email'</span>)</span><br><span class="line">               -&gt;orWhere(<span class="string">'score'</span>, <span class="string">'&gt;'</span>, <span class="string">'1000'</span>);</span><br><span class="line">     &#125;)</span><br><span class="line">     -&gt;get();</span><br><span class="line">DB::table(<span class="string">'users'</span>)</span><br><span class="line">     -&gt;where(<span class="string">'group_id'</span>, <span class="string">'group id'</span>)</span><br><span class="line">     -&gt;where(<span class="function"><span class="keyword">function</span> (<span class="params">$query</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($params[<span class="string">'name'</span>]) &#123;</span><br><span class="line">          $query-&gt;orWhere(<span class="string">'name'</span>, $params[<span class="string">'name'</span>])</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($params[<span class="string">'mobile_number'</span>]) &#123;</span><br><span class="line">          $query-&gt;orWhere(<span class="string">'mobile_number'</span>, $params[<span class="string">'mobile_number'</span>])</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($params[<span class="string">'email'</span>]) &#123;</span><br><span class="line">          $query-&gt;orWhere(<span class="string">'email'</span>, $params[<span class="string">'email'</span>])</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($params[<span class="string">'score'</span>]) &#123;</span><br><span class="line">          $query-&gt;orWhere(<span class="string">'score'</span>, <span class="string">'&gt;'</span>, $params[<span class="string">'score'</span>])</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">     &#125;)</span><br><span class="line">     -&gt;get();</span><br><span class="line">  $orWhere = [];</span><br><span class="line">  <span class="keyword">if</span> ($params[<span class="string">'name'</span>]) &#123;</span><br><span class="line">      $orWhere[] = [<span class="string">'name'</span>, <span class="string">'='</span>, $params[<span class="string">'name'</span>], <span class="string">'OR'</span>];</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">if</span> ($params[<span class="string">'mobile_number'</span>]) &#123;</span><br><span class="line">      $orWhere[] = [<span class="string">'mobile_number'</span>, <span class="string">'='</span>, $params[<span class="string">'mobile_number'</span>], <span class="string">'OR'</span>];</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">if</span> ($params[<span class="string">'email'</span>]) &#123;</span><br><span class="line">      $orWhere[] = [<span class="string">'email'</span>, <span class="string">'='</span>, $params[<span class="string">'email'</span>], <span class="string">'OR'</span>];</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">if</span> ($params[<span class="string">'score'</span>]) &#123;</span><br><span class="line">      $orWhere[] = [<span class="string">'score'</span>, <span class="string">'&gt;'</span>, $params[<span class="string">'score'</span>], <span class="string">'OR'</span>];</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  DB::table(<span class="string">'users'</span>)</span><br><span class="line">     -&gt;where(<span class="string">'group_id'</span>, <span class="string">'group id'</span>)</span><br><span class="line">     -&gt;where($orWhere)</span><br><span class="line">     -&gt;get();</span><br><span class="line">   $orWhere = [];</span><br><span class="line">    <span class="keyword">if</span> ($params[<span class="string">'name'</span>]) &#123;</span><br><span class="line">        $orWhere[<span class="string">'name'</span>] = $params[<span class="string">'name'</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ($params[<span class="string">'mobile_number'</span>]) &#123;</span><br><span class="line">       $orWhere[<span class="string">'mobile_number'</span>] = $params[<span class="string">'mobile_number'</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ($params[<span class="string">'email'</span>]) &#123;</span><br><span class="line">        $orWhere[<span class="string">'email'</span>] = $params[<span class="string">'email'</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ($params[<span class="string">'score'</span>]) &#123;</span><br><span class="line">        $orWhere[] = [<span class="string">'score'</span>, <span class="string">'&gt;'</span>, <span class="number">1000</span>, <span class="string">'OR'</span>];</span><br><span class="line">    &#125;</span><br><span class="line">  DB::table(<span class="string">'users'</span>)</span><br><span class="line">     -&gt;where(<span class="string">'group_id'</span>, <span class="string">'group id'</span>)</span><br><span class="line">     -&gt;where(<span class="function"><span class="keyword">function</span> (<span class="params">$query</span>) <span class="title">use</span> (<span class="params">$orWhere</span>) </span>&#123;</span><br><span class="line">        $query-&gt;orWhere($orWhere);</span><br><span class="line">     &#125;)</span><br><span class="line">     -&gt;get();</span><br><span class="line">  https:<span class="comment">//learnku.com/articles/36743</span></span><br></pre></td></tr></table></figure>
<h3 id="查询封装"><a href="#查询封装" class="headerlink" title="查询封装"></a>查询封装</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">select * <span class="keyword">from</span> posts where title=<span class="string">'xxx'</span> and content=<span class="string">'xxxx'</span>;</span><br><span class="line"></span><br><span class="line">use Illuminate\Http\Request;</span><br><span class="line">use Illuminate\Database\Query\Builder;</span><br><span class="line"></span><br><span class="line">abstract <span class="class"><span class="keyword">class</span> <span class="title">QueryFilter</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    protected $request;</span><br><span class="line">    protected $builder;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">Request $request</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;request = $request;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">apply</span>(<span class="params">Builder $builder</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;builder = $builder;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;init();</span><br><span class="line"></span><br><span class="line">        foreach ($<span class="keyword">this</span>-&gt;filters() <span class="keyword">as</span> $name =&gt; $value) &#123;</span><br><span class="line">            <span class="keyword">if</span>($value)&#123;</span><br><span class="line">                <span class="keyword">if</span> (method_exists($<span class="keyword">this</span>, $name)) &#123;</span><br><span class="line">                    call_user_func_array([$<span class="keyword">this</span>, $name],[$value]);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;builder;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">init</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//子类可以做一些初始化的查询</span></span><br><span class="line">    &#125;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">filters</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;request-&gt;all();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">namespace App\Services\Search\Db\Filter;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PostFilter</span> <span class="keyword">extends</span> <span class="title">QueryFilter</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">title</span>(<span class="params">$title</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;builder-&gt;where(<span class="string">'title'</span>, <span class="string">'like'</span>, <span class="string">"%&#123;$title&#125;%"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">content</span>(<span class="params">$content</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;builder-&gt;where(<span class="string">'content'</span>,<span class="string">'like'</span>, <span class="string">"%&#123;$content&#125;%"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">use DB;</span><br><span class="line"></span><br><span class="line">DB::table(<span class="string">'posts'</span>)-&gt;where(<span class="function"><span class="keyword">function</span>(<span class="params">$query</span>)</span>&#123;</span><br><span class="line">    app(\App\Services\Search\Db\Filter\PostFilter::<span class="class"><span class="keyword">class</span>)-&gt;<span class="title">apply</span>($<span class="title">query</span>)</span>;</span><br><span class="line"></span><br><span class="line">&#125;)-&gt;get();</span><br><span class="line">https:<span class="comment">//learnku.com/articles/36716</span></span><br></pre></td></tr></table></figure>
<h3 id="redis同步到数据库"><a href="#redis同步到数据库" class="headerlink" title="redis同步到数据库"></a>redis同步到数据库</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br></pre></td><td class="code"><pre><span class="line">namespace App\Models\Traits;</span><br><span class="line"></span><br><span class="line">use Redis;</span><br><span class="line">use Carbon\Carbon;</span><br><span class="line"></span><br><span class="line">trait ViewCountsHelper &#123;</span><br><span class="line">    <span class="comment">// 缓存相关</span></span><br><span class="line">    protected $hash_prefix = <span class="string">'topic_view_counts_'</span>;</span><br><span class="line">    protected $field_prefix = <span class="string">'topic_'</span>;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">viewCountIncrement</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 获取今日 Redis 哈希表名称，如：topic_view_counts_2017-10-21</span></span><br><span class="line">        $hash = $<span class="keyword">this</span>-&gt;getHashFromDateString(Carbon::now()-&gt;toDateString());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 字段名称，如：topic_1</span></span><br><span class="line">        $field = $<span class="keyword">this</span>-&gt;getHashField();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 当前阅读数，如果存在就自增，否则就为 1</span></span><br><span class="line">        $count = Redis::hGet($hash, $field);</span><br><span class="line">        <span class="keyword">if</span> ($count) &#123;</span><br><span class="line">            $count++;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $count = <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 数据写入 Redis ，字段已存在会被更新</span></span><br><span class="line">        Redis::hSet($hash, $field, $count);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">syncTopicViewCounts</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 获取昨日的哈希表名称，如：topic_view_counts_2017-10-21</span></span><br><span class="line">        $hash = $<span class="keyword">this</span>-&gt;getHashFromDateString(Carbon::now()-&gt;toDateString());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 从 Redis 中获取所有哈希表里的数据</span></span><br><span class="line">        $counts = Redis::hGetAll($hash);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 如果没有任何数据直接 return</span></span><br><span class="line">        <span class="keyword">if</span> (count($counts) === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 遍历，并同步到数据库中</span></span><br><span class="line">        foreach ($counts <span class="keyword">as</span> $topic_id =&gt; $view_count) &#123;</span><br><span class="line">            <span class="comment">// 会将 `topic_1` 转换为 1</span></span><br><span class="line">            $topic_id = str_replace($<span class="keyword">this</span>-&gt;field_prefix, <span class="string">''</span>, $topic_id);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 只有当话题存在时才更新到数据库中</span></span><br><span class="line">            <span class="keyword">if</span> ($topic = $<span class="keyword">this</span>-&gt;find($topic_id)) &#123;</span><br><span class="line">                $topic-&gt;view_count = $<span class="keyword">this</span>-&gt;attribute[<span class="string">'view_count'</span>] + $view_count;</span><br><span class="line">                $topic-&gt;save();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 以数据库为中心的存储，既已同步，即可删除</span></span><br><span class="line">        Redis::del($hash);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getViewCountAttribute</span>(<span class="params">$value</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 获取今日对应的哈希表名称</span></span><br><span class="line">        $hash = $<span class="keyword">this</span>-&gt;getHashFromDateString(Carbon::now()-&gt;toDateString());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 字段名称，如：topic_1</span></span><br><span class="line">        $field = $<span class="keyword">this</span>-&gt;getHashField();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 三元运算符，优先选择 Redis 的数据，否则使用数据库中</span></span><br><span class="line">        $count = Redis::hGet($hash, $field) ? : $value;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 如果存在的话，返回 数据库中的阅读数 加上 Redis 中的阅读数</span></span><br><span class="line">        <span class="keyword">if</span> ($count) &#123;</span><br><span class="line">            <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;attribute[<span class="string">'view_count'</span>] + $count;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 否则返回 0</span></span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getHashFromDateString</span>(<span class="params">$date</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// Redis 哈希表的命名，如：topic_view_counts_2017-10-21</span></span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hash_prefix . $date;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getHashField</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 字段名称，如：topic_1</span></span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;field_prefix . $<span class="keyword">this</span>-&gt;id;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">在需要此功能的模型中 use Traits\ViewCountsHelper 即可</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">namespace App\Console\Commands;</span><br><span class="line"></span><br><span class="line">use Illuminate\Console\Command;</span><br><span class="line">use App\Models\Topic;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SyncTopicViewCounts</span> <span class="keyword">extends</span> <span class="title">Command</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $signature = <span class="string">'topic:sync-topic-view-counts'</span>;</span><br><span class="line">    protected $description = <span class="string">'将话题 view_count 从 Redis 同步到数据库中'</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Execute the console command.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return mixed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">Topic $topic</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $topic-&gt;syncTopicViewCounts();</span><br><span class="line">        $<span class="keyword">this</span>-&gt;info(<span class="string">"同步成功！"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">namespace App\Http\Controllers;</span><br><span class="line"></span><br><span class="line">use App\Models\Topic;</span><br><span class="line">use Illuminate\Http\Request;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TopicsController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">show</span>(<span class="params">Topic $topic</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $topic-&gt;viewCountIncrement(); <span class="comment">// 自增浏览数</span></span><br><span class="line"></span><br><span class="line">        dd($topic-&gt;view_count); <span class="comment">// 获取浏览数</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32615</span></span><br></pre></td></tr></table></figure>
<h3 id="where-in"><a href="#where-in" class="headerlink" title="where in"></a>where in</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">$where[<span class="string">'status'</span>] = <span class="number">1</span>;</span><br><span class="line">$ids = [<span class="number">1</span>,<span class="number">2</span>];</span><br><span class="line">$where[] = [<span class="function"><span class="keyword">function</span>(<span class="params">$query</span>) <span class="title">use</span> (<span class="params">$ids</span>)</span>&#123;</span><br><span class="line">    $query-&gt;whereIn(<span class="string">'id'</span>, $ids);</span><br><span class="line">&#125;];</span><br><span class="line">$list = User::where($where)</span><br><span class="line">    -&gt;get();</span><br><span class="line">生成 sql 如下</span><br><span class="line"></span><br><span class="line">select * <span class="keyword">from</span> <span class="string">`users`</span> where (<span class="string">`status`</span> = <span class="number">1</span> and (<span class="string">`id`</span> <span class="keyword">in</span> (<span class="number">1</span>, <span class="number">2</span>)))</span><br><span class="line"></span><br><span class="line">$status = <span class="number">1</span>;</span><br><span class="line">$ids = [<span class="number">1</span>,<span class="number">2</span>];</span><br><span class="line">User::when($status, <span class="function"><span class="keyword">function</span> (<span class="params">$query, $status</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $query-&gt;where(<span class="string">'status'</span>, $status);</span><br><span class="line">    &#125;)</span><br><span class="line">        -&gt;when($ids, <span class="function"><span class="keyword">function</span> (<span class="params">$query, $ids</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> $query-&gt;whereIn(<span class="string">'id'</span>, $ids);</span><br><span class="line">        &#125;)</span><br><span class="line">        -&gt;get();</span><br><span class="line">https:<span class="comment">//learnku.com/articles/36964</span></span><br></pre></td></tr></table></figure>
<h3 id="付费文章试读"><a href="#付费文章试读" class="headerlink" title="付费文章试读"></a>付费文章试读</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">对 HTML 进行截断：</span><br><span class="line"></span><br><span class="line">closetags(str_limit($article-&gt;body, <span class="number">1000</span>))</span><br><span class="line">闭合标签：</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">closetags</span>(<span class="params">$html</span>) </span>&#123;</span><br><span class="line">    libxml_use_internal_errors(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">    $dom = <span class="keyword">new</span> \DOMDocument;</span><br><span class="line">    $dom-&gt;loadHTML(<span class="string">'&lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt;'</span> . $html);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Strip wrapping &lt;html&gt; and &lt;body&gt; tags</span></span><br><span class="line">    $mock = <span class="keyword">new</span> \DOMDocument;</span><br><span class="line">    $body = $dom-&gt;getElementsByTagName(<span class="string">'body'</span>)-&gt;item(<span class="number">0</span>);</span><br><span class="line">    foreach ($body-&gt;childNodes <span class="keyword">as</span> $child) &#123;</span><br><span class="line">        $mock-&gt;appendChild($mock-&gt;importNode($child, <span class="literal">true</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> trim($mock-&gt;saveHTML());</span><br><span class="line">&#125;https:<span class="comment">//learnku.com/laravel/t/36859</span></span><br></pre></td></tr></table></figure>
<h3 id="全局记录管理员的所有操作"><a href="#全局记录管理员的所有操作" class="headerlink" title="全局记录管理员的所有操作"></a>全局记录管理员的所有操作</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">namespace App\Providers;</span><br><span class="line">use App\Models\AdminLog;</span><br><span class="line">use Illuminate\Support\Facades\Auth;</span><br><span class="line">use Illuminate\Support\Facades\Event;</span><br><span class="line">use Illuminate\Support\ServiceProvider;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LogServiceProvider</span> <span class="keyword">extends</span> <span class="title">ServiceProvider</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Register services.</span></span><br><span class="line"><span class="comment">     *https://gist.github.com/Hanson/b8916bf13c38336f8eb33e537fab3723</span></span><br><span class="line"><span class="comment">     * @return void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">boot</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        Event::listen(<span class="string">'eloquent.*'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$eventName, $data</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (preg_match(<span class="string">'/eloquent\.(.+):\s(.+)/'</span>, $eventName, $match) === <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">/** $match @val array</span></span><br><span class="line"><span class="comment">            array (</span></span><br><span class="line"><span class="comment">                0 =&gt; 'eloquent.booting: App\\Models\\Groupon',</span></span><br><span class="line"><span class="comment">                1 =&gt; 'booting',</span></span><br><span class="line"><span class="comment">                2 =&gt; 'App\\Models\\Groupon',</span></span><br><span class="line"><span class="comment">            )</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            <span class="comment">// only record when 'created', 'updated', 'deleted'</span></span><br><span class="line">            <span class="keyword">if</span> (!in_array($match[<span class="number">1</span>], [<span class="string">'created'</span>, <span class="string">'updated'</span>, <span class="string">'deleted'</span>])) &#123;</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// only record the admin operation.</span></span><br><span class="line">            <span class="keyword">if</span> (!Auth::guard(<span class="string">'admin'</span>)-&gt;check()) &#123;</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            $model = $data[<span class="number">0</span>];</span><br><span class="line">            $<span class="class"><span class="keyword">class</span> </span>= get_class($model);</span><br><span class="line">            $diff = array_diff_assoc($model-&gt;getOriginal(), $model-&gt;getAttributes());</span><br><span class="line">            $keys = array_keys($diff);</span><br><span class="line">            $data = [];</span><br><span class="line">            foreach ($keys <span class="keyword">as</span> $key) &#123;</span><br><span class="line">                <span class="keyword">if</span> ($key === <span class="string">'updated_at'</span>) &#123;</span><br><span class="line">                    <span class="keyword">continue</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                $data[$key] = [</span><br><span class="line">                    <span class="string">'old'</span> =&gt; $model-&gt;getOriginal($key),</span><br><span class="line">                    <span class="string">'new'</span> =&gt; $model-&gt;getAttributes()[$key]</span><br><span class="line">                ];</span><br><span class="line">            &#125;</span><br><span class="line">            $admin = Auth::guard(<span class="string">'admin'</span>)-&gt;user();</span><br><span class="line">            <span class="comment">// You can create the table with your situation</span></span><br><span class="line">            AdminLog::query()-&gt;create([</span><br><span class="line">                <span class="string">'admin_id'</span> =&gt; $admin-&gt;id,</span><br><span class="line">                <span class="string">'url'</span> =&gt; request()-&gt;fullUrl(),</span><br><span class="line">                <span class="string">'action'</span> =&gt; $match[<span class="number">1</span>], <span class="comment">// updated created deleted</span></span><br><span class="line">                <span class="string">'ip'</span> =&gt; request()-&gt;getClientIp(),</span><br><span class="line">                <span class="string">'model_id'</span> =&gt; $model-&gt;id,</span><br><span class="line">                <span class="string">'model_type'</span> =&gt; $<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line"><span class="class">                '<span class="title">data</span>' </span>=&gt; $data,</span><br><span class="line">                <span class="string">'created_at'</span> =&gt; now(),</span><br><span class="line">            ]);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="Column-not-found-1054-Unknown-column"><a href="#Column-not-found-1054-Unknown-column" class="headerlink" title="Column not found: 1054 Unknown column"></a>Column not found: 1054 Unknown column</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">$res = Model::where(<span class="string">'date'</span>,date(<span class="string">'Ymd'</span>))</span><br><span class="line">-&gt;whereraw(<span class="string">'(a=1 or b=2)'</span>)</span><br><span class="line"></span><br><span class="line">$date=date(<span class="string">'Ymd'</span>);</span><br><span class="line">$res = Model::where(<span class="string">'date'</span>,$date)</span><br><span class="line">-&gt;whereraw(<span class="string">'(a=1 or b=2)'</span>)</span><br><span class="line">提示Column not found: <span class="number">1054</span> Unknown column</span><br><span class="line">$res=Model::where(<span class="string">'date'</span>,$date)</span><br><span class="line">-&gt;where(<span class="function"><span class="keyword">function</span> (<span class="params">$query</span>) <span class="title">use</span>(<span class="params">$date</span>)</span>&#123;</span><br><span class="line">                        $query-&gt;where(<span class="string">'a'</span>, <span class="string">'&gt;'</span>, $date)-&gt;orwhere(<span class="string">'b'</span>, <span class="string">'2'</span>);</span><br><span class="line">                    &#125;)</span><br></pre></td></tr></table></figure>
<h3 id="groupBy-分组查询的分页"><a href="#groupBy-分组查询的分页" class="headerlink" title="groupBy 分组查询的分页"></a>groupBy 分组查询的分页</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">use Illuminate\Pagination\LengthAwarePaginator;</span><br><span class="line">use Illuminate\Support\Facades\DB;</span><br><span class="line">use App\Models\UserHasCard;</span><br><span class="line">...</span><br><span class="line">$page = <span class="number">1</span>;<span class="comment">//当前页</span></span><br><span class="line">$pageSize = <span class="number">10</span>;</span><br><span class="line">$model = UserHasCard::groupBy(<span class="string">'user_id'</span>);</span><br><span class="line">$count = DB::table($model)-&gt;count();</span><br><span class="line">$data = $model-&gt;limit($pageSize)-&gt;offset(($page<span class="number">-1</span>)*$pageSize)-&gt;get();</span><br><span class="line">$list = <span class="keyword">new</span> LengthAwarePaginator($data,$count,$pageSize,$page);</span><br><span class="line">https:<span class="comment">//learnku.com/articles/37239</span></span><br></pre></td></tr></table></figure>
<h3 id="php-artisan-tinker编码"><a href="#php-artisan-tinker编码" class="headerlink" title="php artisan tinker编码"></a>php artisan tinker编码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&gt;&gt;&gt; \App\Ip::where(<span class="string">'zt'</span>, <span class="string">'未付'</span>)-&gt; get();</span><br><span class="line">Illuminate\Database\QueryException <span class="keyword">with</span> message <span class="string">'SQLSTATE[HY000]: General error: 1267 Illegal mix of collations (gbk_chinese_ci,IMPLICIT) and (utf8_unicode_ci,COERCIBLE) for operation '</span>=<span class="string">' (S</span></span><br><span class="line"><span class="string">QL: select * from `ip` where `zt` = 未付)'</span></span><br><span class="line"></span><br><span class="line">\App\Ip::query(<span class="string">'select convert(zt USING gbk) from ip where zt = "未付"'</span>)-&gt;limit(<span class="number">2</span>)-&gt;get()</span><br><span class="line">&gt;&gt;&gt; \App\Ip::query(<span class="string">'select convert(zt USING gbk) where zt = 未付'</span>)-&gt;limit(<span class="number">2</span>)-&gt;get()</span><br><span class="line">select convert(zt USING gbk) <span class="keyword">from</span> <span class="string">`ip`</span> where zt = <span class="string">"未付"</span></span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/37299</span></span><br></pre></td></tr></table></figure>
<h3 id="api文档l5-swagger"><a href="#api文档l5-swagger" class="headerlink" title="api文档l5-swagger"></a>api文档l5-swagger</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> darkaonline/l5-swagger</span><br><span class="line">php artisan vendor:publish --provider <span class="string">"L5Swagger\L5SwaggerServiceProvider"</span></span><br><span class="line">http:<span class="comment">//122.51.155.172:8000/api/documentation</span></span><br><span class="line"></span><br><span class="line">vi app/Http/Controllers/MyController.php</span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @OA\Info(title="Test", version="0.0.1")</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">// 别直接复制这里的注释，缩进破坏了，</span></span><br><span class="line"><span class="comment">// 请上 github 上复制它的用例。这里只作为演示作用</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyController</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @OA\Get(</span></span><br><span class="line"><span class="comment"> *     path="/api/syahi",</span></span><br><span class="line"><span class="comment"> *     @OA\Response(response="200", description="An example resource")</span></span><br><span class="line"><span class="comment"> * )</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">say</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> [<span class="string">'msg'</span> =&gt; <span class="string">'Hello World'</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// Other Code...</span></span><br><span class="line">&#125;</span><br><span class="line">php artisan l5-swagger:generate</span><br><span class="line"></span><br><span class="line">它会生成 API 文档，默认在项目根目录： storage\api-docs\api-docs.json</span><br><span class="line">https:<span class="comment">//learnku.com/articles/37313#reply119615</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-里面的-chunk-分块效率"><a href="#Laravel-里面的-chunk-分块效率" class="headerlink" title="Laravel 里面的 chunk 分块效率"></a>Laravel 里面的 chunk 分块效率</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"> public <span class="function"><span class="keyword">function</span> <span class="title">chunkTest</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 每次处理</span></span><br><span class="line">        $speed = <span class="number">1000</span>;</span><br><span class="line">        <span class="comment">// 进度条</span></span><br><span class="line">        $bar = $<span class="keyword">this</span>-&gt;output-&gt;createProgressBar(TestModel::query()-&gt;count());</span><br><span class="line">        <span class="comment">// 记录开始时间</span></span><br><span class="line">        $timeStart = $<span class="keyword">this</span>-&gt;microtime_float();</span><br><span class="line">        <span class="comment">// chunk 分块处理数据</span></span><br><span class="line">        TestModel::query()-&gt;chunk($speed, <span class="function"><span class="keyword">function</span> (<span class="params">$item</span>) <span class="title">use</span> (<span class="params">$bar, $speed</span>) </span>&#123;</span><br><span class="line">        <span class="comment">// 业务处理逻辑...</span></span><br><span class="line">        <span class="comment">// ....</span></span><br><span class="line">        <span class="comment">// 进度条步进&#123;$speed&#125;步\  $bar-&gt;advance($speed);</span></span><br><span class="line">        &#125;);</span><br><span class="line">        $bar-&gt;finish();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 处理完成，记录结束时间</span></span><br><span class="line">        $timeEnd = $<span class="keyword">this</span>-&gt;microtime_float();</span><br><span class="line">        $time = $timeEnd - $timeStart;</span><br><span class="line">        <span class="comment">// 输出信息</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;info(<span class="string">'chunk用时:'</span>. $time);</span><br><span class="line">    &#125;</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">idTest</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 进度条</span></span><br><span class="line">    $bar = $<span class="keyword">this</span>-&gt;output-&gt;createProgressBar(TestModel::query()-&gt;count());</span><br><span class="line">    $timeStart = $<span class="keyword">this</span>-&gt;microtime_float();</span><br><span class="line">    <span class="comment">// 记录最大的id</span></span><br><span class="line">    $maxId = <span class="number">0</span>;</span><br><span class="line">    <span class="comment">// 每次处理多少条数据</span></span><br><span class="line">    $speed = <span class="number">1000</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">        $models = TestModel::query()</span><br><span class="line">            <span class="comment">// 每次循环加上id条件</span></span><br><span class="line">            -&gt;where(<span class="string">'id'</span>, <span class="string">'&gt;'</span>, $maxId)</span><br><span class="line">            -&gt;limit($speed)</span><br><span class="line">            -&gt;orderBy(<span class="string">'id'</span>)</span><br><span class="line">            -&gt;get();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 处理具体业务逻辑...</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 如果没有数据就代表处理完成，break；</span></span><br><span class="line">        <span class="keyword">if</span> ($models-&gt;isEmpty()) &#123;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 记录下本次的最大id，下次循环时当作条件</span></span><br><span class="line">        $maxId = $models-&gt;max([<span class="string">'id'</span>]);</span><br><span class="line"></span><br><span class="line">        $bar-&gt;advance($speed);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    $timeEnd = $<span class="keyword">this</span>-&gt;microtime_float();</span><br><span class="line">    $time = $timeEnd - $timeStart;</span><br><span class="line">    $bar-&gt;finish();</span><br><span class="line">    $<span class="keyword">this</span>-&gt;info(<span class="string">'id条件用时: '</span>. $time);</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/37541</span></span><br></pre></td></tr></table></figure>
<h3 id="Facade-demo"><a href="#Facade-demo" class="headerlink" title="Facade demo"></a>Facade demo</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">php artisan make:provider MyServiceProvider</span><br><span class="line">namespace App\Providers;</span><br><span class="line">use B;</span><br><span class="line">use Illuminate\Support\ServiceProvider;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TestProvider</span> <span class="keyword">extends</span> <span class="title">ServiceProvider</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Register services.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * [[@return](https://learnku.com/users/31554)](https://learnku.com/users/31554) void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;app-&gt;singleton(<span class="string">'my-facade'</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> B();</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Bootstrap services.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * [[@return](https://learnku.com/users/31554)](https://learnku.com/users/31554) void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">boot</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">B.php</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B</span></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">b</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        echo <span class="string">'this class  B  function a ,you are right~!'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">在 app.php 中 分别加入</span><br><span class="line"><span class="string">'providers'</span>=&gt;[</span><br><span class="line">        ......</span><br><span class="line">        App\Providers\MyServiceProvider::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">]</span><br><span class="line"><span class="string">'aliases'</span> =&gt; [</span><br><span class="line">        ......</span><br><span class="line">        <span class="string">'MyFacade'</span>=&gt;\App\Providers\Facades\MyProvider::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">]</span><br><span class="line">在 app/Providers 文件夹下创建 Facades 文件夹 并创建 MyFacade.php 文件</span><br><span class="line">namespace App\Providers\Facades;</span><br><span class="line">use Illuminate\Support\Facades\Facade;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyFacade</span> <span class="keyword">extends</span> <span class="title">Facade</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getFacadeAccessor</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'my-facade'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">\MyFacade::b();</span><br><span class="line">https:<span class="comment">//learnku.com/articles/36610</span></span><br></pre></td></tr></table></figure>
<h3 id="验证码（Captcha）包乱码"><a href="#验证码（Captcha）包乱码" class="headerlink" title="验证码（Captcha）包乱码"></a>验证码（Captcha）包乱码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/Gregwar/Captcha</span></span><br><span class="line">use Gregwar\Captcha\CaptchaBuilder;</span><br><span class="line">$code = <span class="keyword">new</span> CaptchaBuilder;</span><br><span class="line">$code-&gt;build(<span class="number">102</span>, <span class="number">35</span>);</span><br><span class="line">header(<span class="string">'Content-type: image/jpeg'</span>);</span><br><span class="line">$code-&gt;output();</span><br><span class="line"></span><br><span class="line">我是在laravel中使用，会出现乱码。但是调用CaptchaBuilder::save()正常生成图片。</span><br><span class="line">群里的朋友说，可以在最有加一个exit();我试了一下，能行。 应该是在laravel的控制器中执行完毕会有内容输出，影响到了验证码的正常输出。但是加exit()总不是办法，会影响到session</span><br><span class="line">然后换成ob_end_clean()放在最前面，也能行。 我有一篇博客说过，ob_end_clean()和ob_clean()然后换成ob_clean()放在最前面试了一下，不行。 所以猜测，laravel在控制器结束后，会往缓冲区写点东西。而ob_clean()只清空缓冲区，并没有关闭。 所以无法正常输出！http:<span class="comment">//www.shiguopeng.cn/archives/24</span></span><br></pre></td></tr></table></figure>
<h3 id="laravel权限设计"><a href="#laravel权限设计" class="headerlink" title="laravel权限设计"></a>laravel权限设计</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">用户表 角色表 用户角色表 权限表 权限角色表</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">extends</span> <span class="title">Authenticatable</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    use Notifiable;</span><br><span class="line"> </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * The attributes that should be hidden for arrays.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @var array</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected $hidden = [</span><br><span class="line">        <span class="string">'password'</span>, <span class="string">'remember_token'</span>,</span><br><span class="line">    ];</span><br><span class="line"> </span><br><span class="line">    <span class="comment">// 用户和角色的模型关联关系</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">roles</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;belongsToMany(Role::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="comment">/****************************************</span></span><br><span class="line"><span class="comment">    * 封装一个方法方便使用</span></span><br><span class="line"><span class="comment">    * 1. 需要的权限</span></span><br><span class="line"><span class="comment">    * 2. 遍历当期那用户拥有的所有角色</span></span><br><span class="line"><span class="comment">    * 3. 再通过角色判断是否有当前需要的权限</span></span><br><span class="line"><span class="comment">    ****************************************/</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">hasPermission</span>(<span class="params">$permissionName</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        foreach ($<span class="keyword">this</span>-&gt;roles <span class="keyword">as</span> $role) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($role-&gt;permisssions()-&gt;where(<span class="string">'name'</span>, $permissionName)-&gt;exists()) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">true</span>;;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Role</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">// 用户和角色的模型关联关系</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">users</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;belongsToMany(User::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="comment">// 角色和权限的模型关联关系</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">permissions</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;belongsToMany(Permission::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Permission</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">// 角色和权限的模型关联关系</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">roles</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;belongsToMany(Role::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">http:<span class="comment">//www.shiguopeng.cn/archives/260 </span></span><br><span class="line">https:<span class="comment">//github.com/spatie/laravel-permission</span></span><br></pre></td></tr></table></figure>
<h3 id="路由多版本拆分"><a href="#路由多版本拆分" class="headerlink" title="路由多版本拆分"></a>路由多版本拆分</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 路由加载 多版本</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">mapAppRoutes</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="comment">// 获取路由文件夹下的所有目录和文件</span></span><br><span class="line">        foreach (scandir(base_path(<span class="string">'routes'</span>)) <span class="keyword">as</span> $dir)&#123;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//匹配所有以v开头的目录作为版本号</span></span><br><span class="line">            <span class="keyword">if</span>(starts_with($dir,<span class="string">'v'</span>))&#123;</span><br><span class="line"></span><br><span class="line">                <span class="comment">//匹配目录下的php文件</span></span><br><span class="line">                foreach (glob(base_path(<span class="string">'routes/'</span>.$dir) . <span class="string">'/*.php'</span>) <span class="keyword">as</span> $file) &#123;</span><br><span class="line"></span><br><span class="line">                    <span class="comment">//前缀 目录版本号/文件名</span></span><br><span class="line">                    Route::prefix($dir.<span class="string">'/'</span>.basename($file,<span class="string">'.php'</span>))</span><br><span class="line">                        -&gt;middleware(<span class="string">'api'</span>)</span><br><span class="line">                        <span class="comment">//设置命名空间</span></span><br><span class="line">                        -&gt;namespace($<span class="keyword">this</span>-&gt;namespace.<span class="string">'\\'</span>.ucfirst(basename($file,<span class="string">'.php'</span>)))</span><br><span class="line">                        -&gt;group($file);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">路由目录文件如下https:<span class="comment">//learnku.com/articles/37684</span></span><br><span class="line"></span><br><span class="line">├── routes</span><br><span class="line">│   └── v1</span><br><span class="line">│       ├── a.php</span><br><span class="line">│       ├── b.php</span><br><span class="line">│   └── v2</span><br><span class="line">│       ├── c.php</span><br></pre></td></tr></table></figure>
<h3 id="lua-脚本-运行-Redis-命令"><a href="#lua-脚本-运行-Redis-命令" class="headerlink" title="lua 脚本 运行 Redis 命令"></a>lua 脚本 运行 Redis 命令</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">IncrementExistingKeysBy</span> <span class="keyword">extends</span> <span class="title">ScriptCommand</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">      public <span class="function"><span class="keyword">function</span> <span class="title">getKeysCount</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">      </span>&#123;  <span class="comment">// Tell Predis to use all the arguments but the last one as arguments</span></span><br><span class="line">       <span class="comment">// for KEYS. The last one will be used to populate ARGV.\  return -1;</span></span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      public <span class="function"><span class="keyword">function</span> <span class="title">getScript</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">      </span>&#123;  </span><br><span class="line">         <span class="keyword">return</span> &lt;&lt;&lt;LUA</span><br><span class="line">            local cmd, insert = redis.call, table.insert</span><br><span class="line">            local increment, results = ARGV[1], &#123; &#125;</span><br><span class="line"></span><br><span class="line">            for idx, key in ipairs(KEYS) </span><br><span class="line">                 if cmd('exists', key) == 1 then </span><br><span class="line">                     insert(results, idx, cmd('incrby', key, increment))</span><br><span class="line">                 else</span><br><span class="line">                     insert(results, idx, false) end</span><br><span class="line">                end</span><br><span class="line"></span><br><span class="line">            return results</span><br><span class="line">            LUA;</span><br><span class="line">       &#125;</span><br><span class="line">&#125;</span><br><span class="line">调用方式</span><br><span class="line"></span><br><span class="line">$client = new Predis\Client($single_server, array(</span><br><span class="line">    'profile' =&gt; function ($options) &#123;</span><br><span class="line">        $profile = $options-&gt;getDefault('profile');</span><br><span class="line">        $profile-&gt;defineCommand('increxby', 'IncrementExistingKeysBy');</span><br><span class="line"></span><br><span class="line">        return $profile;</span><br><span class="line">    &#125;,</span><br><span class="line">));</span><br><span class="line"></span><br><span class="line">$client-&gt;mset('foo', 10, 'foobar', 100);</span><br><span class="line"></span><br><span class="line">var_export($client-&gt;increxby('foo', 'foofoo', 'foobar', 50));</span><br><span class="line">https://learnku.com/articles/37661</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-使用-Markdown-做文章编辑后台"><a href="#Laravel-使用-Markdown-做文章编辑后台" class="headerlink" title="Laravel 使用 Markdown 做文章编辑后台"></a>Laravel 使用 Markdown 做文章编辑后台</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> chenhua/laravel5-markdown-editor</span><br><span class="line">php artisan vendor:publish --tag=markdown</span><br><span class="line">修改 config/markdowneditor.php 配置文件</span><br><span class="line">&lt;?php</span><br><span class="line"><span class="keyword">return</span> [</span><br><span class="line">    <span class="string">"default"</span>     =&gt; <span class="string">'local'</span>, <span class="comment">//默认返回存储位置url.也可以设置七牛上传路径,阿里云存储文件等等</span></span><br><span class="line">    <span class="string">"dirver"</span>      =&gt; [<span class="string">'local'</span>], <span class="comment">//存储平台 ['local', 'qiniu', 'aliyun']</span></span><br><span class="line">    <span class="string">"connections"</span> =&gt; [</span><br><span class="line">        <span class="string">"local"</span>  =&gt; [</span><br><span class="line">            <span class="string">'prefix'</span> =&gt; <span class="string">'uploads/markdown'</span>, <span class="comment">//本地存储位置，默认uploads</span></span><br><span class="line">        ],</span><br><span class="line">        <span class="string">"qiniu"</span>  =&gt; [</span><br><span class="line">            <span class="string">'access_key'</span> =&gt; <span class="string">''</span>,</span><br><span class="line">            <span class="string">'secret_key'</span> =&gt; <span class="string">''</span>,</span><br><span class="line">            <span class="string">'bucket'</span>     =&gt; <span class="string">''</span>,</span><br><span class="line">            <span class="string">'prefix'</span>     =&gt; <span class="string">''</span>, <span class="comment">//文件前缀 file/of/path</span></span><br><span class="line">            <span class="string">'domain'</span>     =&gt; <span class="string">''</span> <span class="comment">//七牛自定义域名</span></span><br><span class="line">        ],</span><br><span class="line">        <span class="string">"aliyun"</span> =&gt; [</span><br><span class="line">            <span class="string">'ak_id'</span>     =&gt; <span class="string">''</span>,</span><br><span class="line">            <span class="string">'ak_secret'</span> =&gt; <span class="string">''</span>,</span><br><span class="line">            <span class="string">'end_point'</span>  =&gt; <span class="string">''</span>,</span><br><span class="line">            <span class="string">'bucket'</span>    =&gt; <span class="string">''</span>,</span><br><span class="line">            <span class="string">'prefix'</span>    =&gt; <span class="string">''</span>,</span><br><span class="line">        ],</span><br><span class="line">    ],</span><br><span class="line">];</span><br><span class="line"> <span class="comment">//主页显示</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params">Request $request</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> view(<span class="string">'index'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//提交</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">Request $request</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $content = $request-&gt;input(<span class="string">'test-editormd-html-code'</span>);</span><br><span class="line">        $article=Article::create($content);</span><br><span class="line">    &#125;</span><br><span class="line">    https:<span class="comment">//learnku.com/articles/30858</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-的测试与-PHP-Unit"><a href="#Laravel-的测试与-PHP-Unit" class="headerlink" title="Laravel 的测试与 PHP Unit"></a>Laravel 的测试与 PHP Unit</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// /tests/Feature/ExampleTest.php</span></span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace Tests\Feature;</span><br><span class="line"></span><br><span class="line">use Illuminate\Foundation\Testing\RefreshDatabase;</span><br><span class="line">use Tests\TestCase;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ExampleTest</span> <span class="keyword">extends</span> <span class="title">TestCase</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * A basic test example.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">testBasicTest</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;   </span><br><span class="line">        <span class="comment">// /vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php</span></span><br><span class="line">        $response = $<span class="keyword">this</span>-&gt;get(<span class="string">'/'</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// /vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php</span></span><br><span class="line">        $response-&gt;assertStatus(<span class="number">200</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">vendor/bin/phpunit tests/ExampleTest.php</span><br><span class="line">https:<span class="comment">//learnku.com/articles/37699</span></span><br></pre></td></tr></table></figure>
<h3 id="ORM-禁止-update-at-的自动更新"><a href="#ORM-禁止-update-at-的自动更新" class="headerlink" title="ORM 禁止 update_at 的自动更新"></a>ORM 禁止 update_at 的自动更新</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">Call to a member <span class="function"><span class="keyword">function</span> <span class="title">toDateTimeString</span>(<span class="params"></span>) <span class="title">on</span> <span class="title">string</span></span></span><br><span class="line"><span class="function"><span class="title">$user</span> = \<span class="title">App</span>\<span class="title">Models</span>\<span class="title">User</span>::<span class="title">find</span>(<span class="params"><span class="number">1</span></span>);</span></span><br><span class="line"><span class="function">  <span class="title">$user</span>-&gt;<span class="title">sex</span> = 1;</span></span><br><span class="line"><span class="function">  <span class="title">$user</span>-&gt;<span class="title">timestamps</span> = <span class="title">false</span>;</span></span><br><span class="line"><span class="function">  <span class="title">$user</span>-&gt;<span class="title">save</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">  <span class="title">$updatedUser</span> =<span class="title">$user</span>-&gt;<span class="title">fresh</span>(<span class="params"></span>);</span></span><br><span class="line">  return [</span><br><span class="line">  <span class="string">'id'</span> =&gt; $updatedUser-&gt;id,</span><br><span class="line">  <span class="string">'name'</span> =&gt; $updatedUser-&gt;name,</span><br><span class="line">  <span class="string">'sex'</span> =&gt; $updatedUser-&gt;sex,</span><br><span class="line">  <span class="string">'created_at'</span> =&gt; $updatedUser-&gt;created_at-&gt;toDateTimeString(),</span><br><span class="line">  <span class="string">'updated_at'</span> =&gt; $updatedUser-&gt;updated_at-&gt;toDateTimeString(),</span><br><span class="line"> ];</span><br><span class="line">https:<span class="comment">//learnku.com/articles/37717</span></span><br></pre></td></tr></table></figure>
<h3 id="无限级分类"><a href="#无限级分类" class="headerlink" title="无限级分类"></a>无限级分类</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">Schema::create(<span class="string">'categories'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">Blueprint $table</span>) </span>&#123;</span><br><span class="line">    $table-&gt;increments(<span class="string">'id'</span>);</span><br><span class="line">    $table-&gt;integer(<span class="string">'parent_id'</span>);</span><br><span class="line">    $table-&gt;string(<span class="string">'code'</span>);</span><br><span class="line">    $table-&gt;string(<span class="string">'name'</span>);</span><br><span class="line">    $table-&gt;string(<span class="string">'path'</span>);</span><br><span class="line">    $table-&gt;timestamps();</span><br><span class="line">&#125;);</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">childCategory</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(<span class="string">'App\Models\Category'</span>, <span class="string">'parent_id'</span>, <span class="string">'id'</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">allChildrenCategorys</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;childCategory()-&gt;<span class="keyword">with</span>(<span class="string">'allChildrenCategorys'</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//无限极分类测试</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    $category = Category::<span class="keyword">with</span>(<span class="string">'allChildrenCategorys'</span>)-&gt;find(<span class="number">5</span>);</span><br><span class="line">    $re = $category-&gt;allChildrenCategorys;</span><br><span class="line"></span><br><span class="line">    dd($re);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//find一个parent_id = 0就可以</span></span><br><span class="line">https:<span class="comment">//learnku.com/articles/14068/simple-practice-of-laravel-infinite-class-classification</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-在-with-查询中只查询个别字段"><a href="#Laravel-在-with-查询中只查询个别字段" class="headerlink" title="Laravel 在 with 查询中只查询个别字段"></a>Laravel 在 with 查询中只查询个别字段</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">$topics = Topic::limit(<span class="number">2</span>)-&gt;<span class="keyword">with</span>([<span class="string">'user'</span>=&gt;<span class="function"><span class="keyword">function</span>(<span class="params">$query</span>)</span>&#123;</span><br><span class="line">   $query-&gt;select(<span class="string">'id'</span>,<span class="string">'username'</span>);</span><br><span class="line">&#125;])-&gt;get();</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BaseModel</span> <span class="keyword">extends</span> \<span class="title">Eloquent</span></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">scopeWithOnly</span>(<span class="params">$query, $relation, Array $columns</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $query-&gt;<span class="keyword">with</span>([<span class="function"><span class="params">$relation</span> =&gt;</span> <span class="function"><span class="keyword">function</span> (<span class="params">$query</span>) <span class="title">use</span> (<span class="params">$columns</span>)</span>&#123;</span><br><span class="line">            $query-&gt;select(array_merge([<span class="string">'id'</span>], $columns));</span><br><span class="line">        &#125;]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">在我们普通的 Model 类都继承基类：</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Topic</span> <span class="keyword">extends</span> <span class="title">BaseModel</span></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">user</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;belongsTo(<span class="string">'User'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">然后使用就很方便了:https:<span class="comment">//learnku.com/laravel/t/1220/laravel-queries-only-individual-fields-in-with-q</span></span><br><span class="line"></span><br><span class="line">$topics = Topic::limit(<span class="number">2</span>)-&gt;withOnly(<span class="string">'user'</span>, [<span class="string">'username'</span>])-&gt;get();</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">scopeWithOnly</span>(<span class="params">object $query, string $relation, array $columns</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $query-&gt;<span class="keyword">with</span>([<span class="function"><span class="params">$relation</span> =&gt;</span> <span class="function"><span class="keyword">function</span> (<span class="params">$query</span>) <span class="title">use</span> (<span class="params">$columns</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (count($columns) == <span class="number">1</span>) &#123;</span><br><span class="line">                $columns = array_merge([<span class="string">'id'</span>], $columns);</span><br><span class="line">            &#125;</span><br><span class="line">            $query-&gt;select($columns);</span><br><span class="line">        &#125;]);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="同步config"><a href="#同步config" class="headerlink" title="同步config"></a>同步config</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">file_put_contents(base_path() . <span class="string">'/config/cnpscy.php'</span>, <span class="string">'&lt;?php return '</span> . var_export($data_list, <span class="literal">true</span>) . <span class="string">';'</span>);</span><br><span class="line">$data_list 数组的 key =&gt; value 格式即可。</span><br><span class="line"></span><br><span class="line">我是存放 config 下面的 cnpscy</span><br><span class="line"></span><br><span class="line">读取：</span><br><span class="line"></span><br><span class="line">config(<span class="string">'cnpscy.key'</span>)</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/38053</span></span><br></pre></td></tr></table></figure>
<h3 id="删除MySQL数据"><a href="#删除MySQL数据" class="headerlink" title="删除MySQL数据"></a>删除MySQL数据</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">$start =  time();</span><br><span class="line"><span class="keyword">while</span>(<span class="literal">true</span>) &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="keyword">if</span>(time() - $start &gt; <span class="number">100</span>) &#123;</span><br><span class="line">                   <span class="comment">// break;</span></span><br><span class="line">                    \DB::disconnect(<span class="string">'mysql::write'</span>);<span class="comment">//断开重新连接</span></span><br><span class="line">                    $sql=<span class="string">"SET SESSION wait_timeout=65535"</span>;</span><br><span class="line">                    \DB::connection(<span class="string">'mysql::write'</span>)-&gt;select($sql);</span><br><span class="line">                    $start = time();</span><br><span class="line">                &#125;</span><br><span class="line">                \DB::connection(<span class="string">'mysql::write'</span>)-&gt;where(<span class="string">'id'</span>,<span class="string">'&gt;'</span>, <span class="number">0</span>)-&gt;<span class="keyword">delete</span>();</span><br><span class="line">            &#125;<span class="keyword">catch</span> (\Exception $e) &#123;</span><br><span class="line">                sleep(<span class="number">1</span>);</span><br><span class="line">                <span class="comment">//exit();</span></span><br><span class="line">                \DB::reconnect(<span class="string">'mysql::write'</span>);</span><br><span class="line">            &#125;</span><br></pre></td></tr></table></figure>
<h3 id="composer-支持laravel"><a href="#composer-支持laravel" class="headerlink" title="composer 支持laravel"></a>composer 支持laravel</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/shanyul/array2xml</span></span><br><span class="line">cat composer.json</span><br><span class="line"><span class="string">"extra"</span>: &#123;</span><br><span class="line">        <span class="string">"laravel"</span>: &#123;</span><br><span class="line">            <span class="string">"providers"</span>: [</span><br><span class="line">                <span class="string">"Shanyuliang\\Array2xml\\ServiceProvider"</span></span><br><span class="line">            ]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">ServiceProvider.php</span><br><span class="line">namespace Shanyuliang\Array2xml;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ServiceProvider</span> <span class="keyword">extends</span> \<span class="title">Illuminate</span>\<span class="title">Support</span>\<span class="title">ServiceProvider</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $defer = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;app-&gt;singleton(Array2xml::<span class="class"><span class="keyword">class</span>, <span class="title">function</span>()</span>&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> Array2xml();</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        $<span class="keyword">this</span>-&gt;app-&gt;alias(Array2xml::<span class="class"><span class="keyword">class</span>, '<span class="title">array2xml</span>')</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">provides</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        return [Array2xml::class, 'array2xml'];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">getXml</span>(<span class="params"></span>) </span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $arr = [</span><br><span class="line">        <span class="string">'aaa'</span> =&gt; [</span><br><span class="line">            <span class="string">'bbb'</span> =&gt; <span class="string">'ccc'</span>,</span><br><span class="line">            <span class="string">'eee'</span> =&gt; <span class="string">'fff'</span></span><br><span class="line">        ]</span><br><span class="line">    ];</span><br><span class="line">    $response = app(<span class="string">'array2xml'</span>)-&gt;generate($arr);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Eloquent-Collection-中获-id-数组"><a href="#Eloquent-Collection-中获-id-数组" class="headerlink" title="Eloquent Collection 中获 id 数组"></a>Eloquent Collection 中获 id 数组</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 返回ID数组</span></span><br><span class="line">$permissionIDs = $role-&gt;permissions-&gt;pluck(<span class="string">'id'</span>); </span><br><span class="line"><span class="comment">//与上述结果相同。甚至相同的字符数:)</span></span><br><span class="line">$permissionIDs = $role-&gt;permissions-&gt;modelKeys();</span><br><span class="line">$allPermissions = Permission::pluck(<span class="string">'id'</span>);</span><br><span class="line">$permissions = $role-&gt;permissions-&gt;pluck(<span class="string">'id'</span>);</span><br><span class="line">$allPermissions = Permission::all()-&gt;modelKeys();</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/39007</span></span><br></pre></td></tr></table></figure>
<h3 id="hasMany-来处理『无限极分类』"><a href="#hasMany-来处理『无限极分类』" class="headerlink" title="hasMany 来处理『无限极分类』"></a>hasMany 来处理『无限极分类』</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">Schema::create(<span class="string">'categories'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">Blueprint $table</span>) </span>&#123;</span><br><span class="line">    $table-&gt;bigIncrements(<span class="string">'id'</span>);</span><br><span class="line">    $table-&gt;string(<span class="string">'name'</span>);</span><br><span class="line">    $table-&gt;unsignedBigInteger(<span class="string">'category_id'</span>)-&gt;nullable();</span><br><span class="line">    $table-&gt;foreign(<span class="string">'category_id'</span>)-&gt;references(<span class="string">'id'</span>)-&gt;on(<span class="string">'categories'</span>);</span><br><span class="line">    $table-&gt;timestamps();</span><br><span class="line">&#125;);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Category</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">categories</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(Category::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">childrenCategories</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(Category::<span class="class"><span class="keyword">class</span>)-&gt;<span class="title">with</span>('<span class="title">categories</span>')</span>;</span><br><span class="line">&#125;</span><br><span class="line">调用 Category::<span class="keyword">with</span>(‘categories’)，将得到下级 “子分类”，但是通过 Category::<span class="keyword">with</span>(‘childrenCategories’) 将能帮你实现无限极。</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $categories = Category::whereNull(<span class="string">'category_id'</span>)</span><br><span class="line">        -&gt;<span class="keyword">with</span>(<span class="string">'childrenCategories'</span>)</span><br><span class="line">        -&gt;get();</span><br><span class="line">    <span class="keyword">return</span> view(<span class="string">'categories'</span>, compact(<span class="string">'categories'</span>));</span><br><span class="line">&#125;</span><br><span class="line">resources/views/categories.blade.php</span><br><span class="line">&lt;ul&gt;</span><br><span class="line">    @foreach ($categories <span class="keyword">as</span> $category)</span><br><span class="line">        &lt;li&gt;&#123;&#123; $category-&gt;name &#125;&#125;&lt;<span class="regexp">/li&gt;</span></span><br><span class="line"><span class="regexp">        &lt;ul&gt;</span></span><br><span class="line"><span class="regexp">        @foreach ($category-&gt;childrenCategories as $childCategory)</span></span><br><span class="line"><span class="regexp">            @include('child_category', ['child_category' =&gt; $childCategory])</span></span><br><span class="line"><span class="regexp">        @endforeach</span></span><br><span class="line"><span class="regexp">        &lt;/u</span>l&gt;</span><br><span class="line">    @endforeach</span><br><span class="line">&lt;<span class="regexp">/ul&gt;</span></span><br><span class="line"><span class="regexp"> resources/</span>views/admin/child_category.blade.php</span><br><span class="line">&lt;li&gt;&#123;&#123; $child_category-&gt;name &#125;&#125;&lt;<span class="regexp">/li&gt;</span></span><br><span class="line"><span class="regexp">@if ($child_category-&gt;categories)</span></span><br><span class="line"><span class="regexp">    &lt;ul&gt;</span></span><br><span class="line"><span class="regexp">        @foreach ($child_category-&gt;categories as $childCategory)</span></span><br><span class="line"><span class="regexp">            @include('child_category', ['child_category' =&gt; $childCategory])</span></span><br><span class="line"><span class="regexp">        @endforeach</span></span><br><span class="line"><span class="regexp">    &lt;/u</span>l&gt;</span><br><span class="line">@endif</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/38977</span></span><br></pre></td></tr></table></figure>
<h3 id="路由不生效"><a href="#路由不生效" class="headerlink" title="路由不生效"></a>路由不生效</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">Effect 为正常控制器补录  Effect_back 为旧的</span><br><span class="line"></span><br><span class="line">使用 find . -name <span class="string">"*.php"</span> |xargs grep -i <span class="string">"Effect_back"</span> 发现了问题所在</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">./vendor/composer/autoload_classmap.php:    <span class="string">'App\\Http\\Controllers\\Effect\\ActivityController'</span> =&gt; $baseDir . <span class="string">'/app/Http/Controllers/Effect_back/ActivityController.php'</span>,</span><br><span class="line">此问题应该是在你有新旧文件时   执行过 composer dump-autoload  所导致旧的文件自动加载进来  并进行了缓存</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">composer clear-cache</span><br><span class="line"></span><br><span class="line">composer dump-autoload</span><br><span class="line">https:<span class="comment">//learnku.com/articles/39287</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-Eloquent"><a href="#Laravel-Eloquent" class="headerlink" title="Laravel Eloquent"></a>Laravel Eloquent</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">$user = User::where(<span class="string">'name'</span>, <span class="string">'wuqinqiang'</span>)-&gt;first();</span><br><span class="line">$user-&gt;age = <span class="number">18</span>;</span><br><span class="line">$user-&gt;wechat-&gt;nickName = <span class="string">"Remember"</span>;</span><br><span class="line">$user-&gt;save()</span><br><span class="line">那么此时，会保存用户信息，但是并不会保存对应 Wechat 的关联信息，想让它一起保存咋么办？也很简单。把 save 换成 push 即可。</span><br><span class="line"></span><br><span class="line">$user-&gt;push();</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">boot</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        parent::boot();</span><br><span class="line">        <span class="keyword">static</span>::creating(<span class="function"><span class="keyword">function</span> (<span class="params">$model</span>) </span>&#123;</span><br><span class="line">           <span class="comment">//业务逻辑</span></span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">那要是不想在操作时触发任何事件则么办？你可以执行一个回调函数而无需触发任何事件:</span><br><span class="line"></span><br><span class="line">$user = User::withoutEvents(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line"><span class="keyword">return</span> <span class="comment">//保存或者更新代码</span></span><br><span class="line">&#125;);</span><br><span class="line">    </span><br><span class="line">    $ user = User :: where(<span class="string">'name'</span>,<span class="string">'wuqinqiang'</span>)-&gt;first();</span><br><span class="line">    $ user-&gt; isDirty（）; <span class="comment">// 还没被动过 返回 false</span></span><br><span class="line">    $ user-&gt; name =<span class="string">'curry'</span>;  </span><br><span class="line">    $ user-&gt; isDirty（）; <span class="comment">//模型被动过了 返回true </span></span><br><span class="line">    $ user-&gt; isDirty（<span class="string">'email'</span>）; <span class="comment">// 可以传入参数  验证某个参数是否被动过，这个我还没碰过 当然 false</span></span><br><span class="line">    $ user-&gt; isDirty（<span class="string">'name'</span>）; <span class="comment">// true </span></span><br><span class="line">    如果你还想具体了解到哪些是被动过了，可以使用:</span><br><span class="line">    </span><br><span class="line">    $user-&gt;getDirty();  你将得到一个数组 [<span class="string">"name"</span> =&gt; <span class="string">"curry"</span>]</span><br><span class="line">    $ user = User :: where(<span class="string">'name'</span>,<span class="string">'wuqinqiang'</span>)-&gt;first();</span><br><span class="line">    $user-&gt;name = <span class="string">'curry'</span>;</span><br><span class="line">    $user-&gt;getOriginal(); <span class="comment">// 返回数组 "name" =&gt; "wuqinqiang"</span></span><br><span class="line">    $user-&gt;getOriginal(<span class="string">'name'</span>); <span class="comment">//指定要看哪个属性的原值  "wuqinqiang"</span></span><br><span class="line">    https:<span class="comment">//learnku.com/articles/39263</span></span><br></pre></td></tr></table></figure>
<h3 id="自定义-Artisan-命令"><a href="#自定义-Artisan-命令" class="headerlink" title="自定义 Artisan 命令"></a>自定义 Artisan 命令</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// routes/console.php</span></span><br><span class="line">Artisan::command(<span class="string">'password:reset &#123;userId&#125; &#123;--sendEmail&#125;'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="comment">// 第一个参数是命令的名称</span></span><br><span class="line">    <span class="comment">// 执行一些操作 ，比如重置用户密码 ...</span></span><br><span class="line">&#125;)</span><br><span class="line">protected $signature = <span class="string">'password:reset &#123;userId&#125; &#123;--sendEmail&#125;'</span>;</span><br><span class="line"> <span class="comment">// 定义 ：password:reset &#123;userId&#125;</span></span><br><span class="line">    <span class="comment">// 执行 ：php artisan password:reset 5</span></span><br><span class="line"></span><br><span class="line">    $<span class="keyword">this</span>-&gt;argument(); <span class="comment">// 返回一个包含所有参数的数组（ 第一个元素是命令名称 ）</span></span><br><span class="line">    <span class="comment">// [</span></span><br><span class="line">    <span class="comment">//     "command" =&gt; "password:reset",</span></span><br><span class="line">    <span class="comment">//     "userId" =&gt; "5"</span></span><br><span class="line">    <span class="comment">// ]</span></span><br><span class="line"></span><br><span class="line">    $<span class="keyword">this</span>-&gt;argument(<span class="string">'userId'</span>);</span><br><span class="line">    <span class="comment">// "5"</span></span><br><span class="line"><span class="comment">// 定义 ：password:reset &#123;--userId=&#125;</span></span><br><span class="line">    <span class="comment">// 执行 ：php artisan password:reset --userId=5</span></span><br><span class="line"></span><br><span class="line">    $<span class="keyword">this</span>-&gt;option(); <span class="comment">// 返回一个包含所有选项的数组</span></span><br><span class="line">    <span class="comment">// [</span></span><br><span class="line">    <span class="comment">//     "userId" =&gt; "5",</span></span><br><span class="line">    <span class="comment">//     "help" =&gt; false,</span></span><br><span class="line">    <span class="comment">//     "quiet" =&gt; false,</span></span><br><span class="line">    <span class="comment">//     "verbose" =&gt; false,</span></span><br><span class="line">    <span class="comment">//     "version" =&gt; false,</span></span><br><span class="line">    <span class="comment">//     "ansi" =&gt; false,</span></span><br><span class="line">    <span class="comment">//     "no-interaction" =&gt; false,</span></span><br><span class="line">    <span class="comment">//     "env" =&gt; null</span></span><br><span class="line">    <span class="comment">// ]</span></span><br><span class="line">    $<span class="keyword">this</span>-&gt;option(<span class="string">'userId'</span>);</span><br><span class="line">    <span class="comment">// "5"</span></span><br><span class="line"><span class="comment">// 提示用户输入</span></span><br><span class="line">$email = $<span class="keyword">this</span>-&gt;ask(<span class="string">"你的邮箱是什么？"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 提示用户输入 ，但用 "*" 隐藏输入内容</span></span><br><span class="line">$password = $<span class="keyword">this</span>-&gt;secret(<span class="string">"你的数据库密码是什么？"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 提示用户输入是 / 否 ，返回布尔值 ，除了按 y 和 Y ，其他的输入都会返回 false</span></span><br><span class="line"><span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;confirm(<span class="string">"你确定要重置用户的密码？"</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 提示用户选择选项 ，如果用户没有选择 ，默认值就是最后一个选项</span></span><br><span class="line">$tips = <span class="string">'你希望在每天什么时间备份数据库？'</span></span><br><span class="line">$options = array(<span class="string">"08:00"</span>, <span class="string">"20:00"</span>, <span class="string">"12:00"</span>);</span><br><span class="line">$<span class="keyword">default</span> = <span class="number">0</span>;</span><br><span class="line">$index = $<span class="keyword">this</span>-&gt;choice($tips, $options, $<span class="keyword">default</span>);</span><br><span class="line"><span class="comment">// 这里注意返回的是用户选择的 key ，而不是 value</span></span><br><span class="line">print_r($optios[$index]);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 还可以使用关联数组</span></span><br><span class="line">$tips = <span class="string">'你现在有两个选择'</span>;</span><br><span class="line">$options = array(<span class="string">"a"</span> =&gt; <span class="string">"自己脱"</span>, <span class="string">"b"</span> =&gt; <span class="string">"我帮你脱"</span>);</span><br><span class="line">$<span class="keyword">default</span> = <span class="string">"b"</span>;</span><br><span class="line">$index = $<span class="keyword">this</span>-&gt;choice($tips, $options, $<span class="keyword">default</span>);</span><br><span class="line">print_r($optios[$index]);</span><br><span class="line">$headers = array(<span class="string">'name'</span>, <span class="string">'email'</span>);</span><br><span class="line">$data = array(</span><br><span class="line">    array(<span class="string">'马云'</span>, <span class="string">'jack_ma@aliyun.com'</span>),</span><br><span class="line">    array(<span class="string">'马化腾'</span>, <span class="string">'pony@qq.com'</span>)</span><br><span class="line">);</span><br><span class="line"><span class="comment">// $data = App\User::all(['name', 'email'])-&gt;toArray();</span></span><br><span class="line">$<span class="keyword">this</span>-&gt;table($headers, $data);</span><br><span class="line"><span class="comment">// 将进度条分为 10 份</span></span><br><span class="line">$total = <span class="number">10</span>;</span><br><span class="line"><span class="comment">// 创建进度条</span></span><br><span class="line">$<span class="keyword">this</span>-&gt;output-&gt;progressStart($total);</span><br><span class="line"><span class="comment">// 循环</span></span><br><span class="line"><span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; $total; $i++) &#123;</span><br><span class="line">    sleep(<span class="number">1</span>);</span><br><span class="line">    <span class="comment">// 进度条步进</span></span><br><span class="line">    $<span class="keyword">this</span>-&gt;output-&gt;progressAdvance();</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 结束进度条</span></span><br><span class="line">$<span class="keyword">this</span>-&gt;output-&gt;progressFinish();</span><br><span class="line"># 2</span><br><span class="line"><span class="number">2</span>/<span class="number">10</span> [=====&gt;----------------------]  <span class="number">20</span>%</span><br><span class="line"># 10</span><br><span class="line"><span class="number">10</span>/<span class="number">10</span> [============================] <span class="number">100</span>%</span><br><span class="line"><span class="comment">// Artisan::call()</span></span><br><span class="line">Route::get(<span class="string">'test-artisan'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; </span><br><span class="line">    $exitCode = Artisan::call(<span class="string">"password:reset"</span>, [</span><br><span class="line">        <span class="string">'userId'</span> =&gt; <span class="number">15</span>, <span class="string">'--sendEmail'</span> =&gt; <span class="literal">true</span></span><br><span class="line">    ])</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// $this-&gt;call() 或者 $this-&gt;callSilent()</span></span><br><span class="line">Route::get(<span class="string">'test-artisan'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; </span><br><span class="line">    <span class="comment">// 这个 callSilent() 有什么区别我也不太懂</span></span><br><span class="line">    $exitCode = $<span class="keyword">this</span>-&gt;callSilent(<span class="string">"password:reset"</span>, [</span><br><span class="line">        <span class="string">'userId'</span> =&gt; <span class="number">15</span>, <span class="string">'--sendEmail'</span> =&gt; <span class="literal">true</span></span><br><span class="line">    ])</span><br><span class="line">&#125;)https:<span class="comment">//learnku.com/articles/39394</span></span><br></pre></td></tr></table></figure>
<h3 id="递归子级"><a href="#递归子级" class="headerlink" title="递归子级"></a>递归子级</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">在 model 里面增加子级关联</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">child</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(self::<span class="class"><span class="keyword">class</span>,'<span class="title">pid</span>')</span>;</span><br><span class="line">    &#125;</span><br><span class="line">在 model 里面增加子级递归关联</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">childRecursive</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;child()-&gt;<span class="keyword">with</span>(<span class="string">'childRecursive'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">调用的时候建议用 <span class="keyword">with</span> 方法预加载https:<span class="comment">//learnku.com/articles/39687</span></span><br><span class="line"></span><br><span class="line">    $menu = Menu::<span class="keyword">with</span>(<span class="string">'childRecursive'</span>)-&gt;find($id);</span><br></pre></td></tr></table></figure>
<p><a href="http://www.siya89.com/blog/laravel%205.5%20blog%20eighth" target="_blank" rel="noopener">laravel5.5 博客第八篇</a></p>
<p><a href="https://learnku.com/articles/39723" target="_blank" rel="noopener">多商户laravel vue开源商城 </a></p>
<p><a href="https://github.com/guojiangclub/ecommerce-open-api" target="_blank" rel="noopener">基于 Laravel + swoole + 小程序的开源电商系统</a></p>
<p><a href="https://learnku.com/laravel/t/39571" target="_blank" rel="noopener">Laravel API 错误处理</a></p>
<p><a href="https://github.com/cranux/LaravelDevTemplate" target="_blank" rel="noopener">laravel + swoole + adminlte 高效开发模板</a></p>
<p><a href="https://learnku.com/articles/38969" target="_blank" rel="noopener">Laravel 项目 伪静态分页处理</a></p>
<p><a href="https://learnku.com/articles/21372" target="_blank" rel="noopener">更快的找到自己所需的模型关联类型</a></p>
<p><a href="https://www.shanyuliang.com/archives/246" target="_blank" rel="noopener">Laravel-dompdf优雅导出PDF</a></p>
<p><a href="https://github.com/shanyul/photosLive" target="_blank" rel="noopener">Laravel + Vue + Oss 搭建的图片墙小站</a></p>
<p><a href="https://learnku.com/articles/38067" target="_blank" rel="noopener">Laravel editor.md 支持截图 / 粘贴上传图片</a></p>
<p><a href="https://www.shanyuliang.com/archives/286" target="_blank" rel="noopener"> ConsoleTVs/Charts 简单快速绘制统计图</a></p>
<p><a href="https://learnku.com/laravel/t/38446" target="_blank" rel="noopener">91 个常见的 Laravel 面试题和答案</a></p>
<p><a href="http://sakyavarro.cn/post/%E8%AE%B0%E5%BD%95%E5%B0%86%E4%B8%80%E4%B8%AA%20Laravel%205.2%20%E9%A1%B9%E7%9B%AE%E5%8D%87%E7%BA%A7%E8%87%B3%205.8%20%E7%9A%84%E8%BF%87%E7%A8%8B" target="_blank" rel="noopener"> Laravel 5.2 项目升级至 5.8 的过程</a></p>
<p><a href="https://learnku.com/articles/37478" target="_blank" rel="noopener"> Laravel 中实现「福勒的货币设计模式」</a></p>
<p><a href="https://gitee.com/uknowfantasy/FantasyCode.git" target="_blank" rel="noopener">Laravel6.0 框架和 semantic-ui 开发的一个个人博客社区网站</a></p>
<p><a href="https://leoyang90.gitbooks.io/laravel-source-analysis/content/" target="_blank" rel="noopener">laravel 源码详解</a></p>
<p><a href="https://github.com/xiaohuilam/searching" target="_blank" rel="noopener">仿 Laravel-china 的搜索包</a></p>
<p><a href="https://learnku.com/articles/37550" target="_blank" rel="noopener">Laravel 5.5 + H+UI 框架的权限管理后台</a></p>
<p><a href="https://learnku.com/articles/36012" target="_blank" rel="noopener">Laravel 服务容器</a></p>
<p><a href="https://github.com/pudongping/larablog" target="_blank" rel="noopener">博客支持 Markdown,支持 RBAC 权限管理</a></p>
<p><a href="https://learnku.com/articles/30982" target="_blank" rel="noopener">三分钟从零部署 Laravel 应用到阿里云 ECS</a></p>
<p><a href="https://learnku.com/articles/37459" target="_blank" rel="noopener">Laravel+easywechat 实现公众号微信支付</a></p>
<p><a href="https://learnku.com/articles/37163" target="_blank" rel="noopener">Laravel-Gii 可视化代码生成工具</a></p>
<p><a href="https://gitee.com/neilyoz/laravel-swoole-chat" target="_blank" rel="noopener">Laravel + Vue + Swoole 实现的简单 Web 聊天</a></p>
<p><a href="https://gitee.com/obamajs/php-base-container" target="_blank" rel="noopener">laravel容器</a></p>
<p><a href="https://learnku.com/articles/35675" target="_blank" rel="noopener">Laravel MongoDB 数据库查询</a></p>
<p><a href="https://learnku.com/articles/34959" target="_blank" rel="noopener"> Laravel 框架 5.1 升级到 5.5 </a></p>
<p><a href="https://github.com/viest/php-ext-excel-export" target="_blank" rel="noopener">php excel</a></p>
<p><a href="https://learnku.com/courses/laravel-performance/6.x" target="_blank" rel="noopener">Laravel 性能优化入门</a></p>
<p><a href="https://learnku.com/articles/36843" target="_blank" rel="noopener">安装 laradock 以及运行 Laravel 项目</a></p>
<p><a href="https://github.com/FireLustre/php-dfa-sensitive" target="_blank" rel="noopener">过滤敏感词汇扩展</a></p>
<p><a href="https://learnku.com/articles/29049#871bbd" target="_blank" rel="noopener">Windows 环境下安装 Laravel</a></p>
<p><a href="https://github.com/moell-peng/laravel-layui-admin" target="_blank" rel="noopener">Laravel-layui-admin : 支持 Laravel6.0 的 RBAC 后台系统</a></p>
<p><a href="https://github.com/telanflow/laravel-binlog" target="_blank" rel="noopener">Laravel-Binlog 扩展</a></p>
<p><a href="https://learnku.com/articles/36574" target="_blank" rel="noopener"> Laravel 自带消息模块搭建小程序实时推送消息</a></p>
<p><a href="https://github.com/shulidata/easyalipay" target="_blank" rel="noopener">基于支付宝 OpenAPI 开源的 PHP 语言版本 SDK</a></p>
<p><a href="https://www.gitbook.com/book/leoyang90/laravel-source-analysis" target="_blank" rel="noopener">gitbook 的 Laravel 源码详解</a></p>
<p><a href="https://cdn.learnku.com/uploads/images/201911/15/39249/so44qFVX1y.png!/fw/1240" target="_blank" rel="noopener">Application 类 make 方法调用栈流程图</a></p>
<p><a href="https://github.com/kong-qi/kongqi_laravel_admin" target="_blank" rel="noopener">快速laravel后台管理系统</a></p>
<p><a href="https://gitee.com/obamajs/php-websocket-base-implemention" target="_blank" rel="noopener">PHP 实现 Websocket 协议</a></p>
<p><a href="https://learnku.com/articles/35992" target="_blank" rel="noopener">Laravel-S 项目之初体验</a></p>
<p><a href="https://github.com/tsmliyun/laravel_quick_admin" target="_blank" rel="noopener">基于laravel5.5 + H-ui开发的一款基础后台管理系统</a></p>
<p><a href="https://github.com/kong-qi/kongqi_laravel_admin" target="_blank" rel="noopener">快速laravel后台管理系统</a></p>
<p><a href="https://github.com/imnotdoubi/laravel-admin.git" target="_blank" rel="noopener">Laravel-admin 码的电子商务前后端系统</a></p>
<p><a href="https://learnku.com/laravel/t/32401" target="_blank" rel="noopener">Laravel+vue 个人博客https://github.com/sweida/laravel-blog-api/</a></p>
<p><a href="https://github.com/bobo1576554465/laravel-source-analysis" target="_blank" rel="noopener">详解 laravel 源码 https://leoyang90.gitbooks.io/laravel-source-analysis/content/</a></p>
<p><a href="https://www.kancloud.cn/martist/ma_zhao_liu/370443" target="_blank" rel="noopener">web开发者知识体系电子书</a></p>
<p><a href="https://learnku.com/articles/35672" target="_blank" rel="noopener"> EasyWechat 和 Laravel notification 发送微信小程序模板消息</a></p>
<p><a href="https://learnku.com/articles/35747" target="_blank" rel="noopener">QQ 登录、微信登录、微博登录、GitHub 登录</a></p>
<p><a href="https://learnku.com/articles/35644" target="_blank" rel="noopener">快速定位无用路由</a></p>
<p><a href="https://learnku.com/laravel/t/35638" target="_blank" rel="noopener">Laravel 上将图片上传到阿里云 OSS</a></p>
<p><a href="https://learnku.com/articles/35592" target="_blank" rel="noopener">快速记忆php系统数组函数</a></p>
<p><a href="https://learnku.com/laravel/t/35561" target="_blank" rel="noopener">附近的店铺</a></p>
<p><a href="https://github.com/snower/forsun-laravel" target="_blank" rel="noopener">高性能的定时调度服务Forsun的Laravel组件</a></p>
<p><a href="https://www.hongfs.cn/2019/06/php/laravel/laravel-query-nearby-data/" target="_blank" rel="noopener">Laravel 查询附近的数据</a></p>
<p><a href="https://github.com/medcl/elasticsearch-rtf" target="_blank" rel="noopener">elasticsearch中文发行版</a></p>
<p><a href="https://www.hongfs.cn/2018/12/php/laravel/laravel-multi-level-linkage/" target="_blank" rel="noopener">多级联动</a></p>
<p><a href="https://learnku.com/articles/30812" target="_blank" rel="noopener">Laravel 下 Elasticsearch/Algolia 全文搜索</a></p>
<p><a href="https://learnku.com/articles/35377" target="_blank" rel="noopener">Phpstorm 开启 Laravel 代码提示</a></p>
<p><a href="https://github.com/shockerli/php-awesome" target="_blank" rel="noopener">PHP 最优秀资源的整理汇集</a></p>
<p><a href="https://learnku.com/articles/35348" target="_blank" rel="noopener">腾讯地图搜索</a></p>
<p><a href="https://learnku.com/articles/34959" target="_blank" rel="noopener">Laravel 框架 5.1 升级到 5.5</a></p>
<p><a href="https://learnku.com/articles/35101" target="_blank" rel="noopener">Laravel Authorization：支持 ACL、RBAC、ABAC 等模型的授权库</a></p>
<p><a href="https://learnku.com/laravel/t/35080" target="_blank" rel="noopener">PSR-12 编码规范扩充</a></p>
<p><a href="https://learnku.com/articles/30812" target="_blank" rel="noopener">Laravel 下 Elasticsearch/Algolia 全文搜索 使用案例</a></p>
<p> <a href="https://learnku.com/articles/6207/lightweight-full-text-retrieval-engine-tntsearch-and-chinese-word-segmentation" target="_blank" rel="noopener">轻量级全文检索引擎 TNTSearch 和中文分词</a></p>
<p><a href="https://baijunyao.com/article/154" target="_blank" rel="noopener">laravel下TNTSearch+jieba-php实现中文全文搜索</a></p>
<p><a href="http://www.putyy.com/article/32" target="_blank" rel="noopener">postman使用记录</a></p>
<p><a href="https://github.com/jianyan74/rageframe2" target="_blank" rel="noopener">免费开源的在线文档管理插件</a></p>
<p><a href="https://gitee.com/leiyong3/laravel_blog" target="_blank" rel="noopener">诗词博客</a></p>
<p><a href="https://thans.cn/mirror/elasticsearch.html" target="_blank" rel="noopener">Elasticsearch 国内镜像下载站</a></p>
<p><a href="https://github.com/ChinaBygones/PHP-DouyinRobot" target="_blank" rel="noopener">PHP抖音机器人</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/15/那些在国内还能使用的谷歌产品/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/10/15/那些在国内还能使用的谷歌产品/" itemprop="url">那些在国内还能使用的谷歌产品</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-10-15T20:33:01+08:00">
                2019-10-15
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.4k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  6 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>前几天是谷歌成立21周年纪念日  <a href="https://www.google.com/doodles/googles-21st-birthday" target="_blank" rel="noopener">https://www.google.com/doodles/googles-21st-birthday</a> ，感谢谷歌给互联网带来的便利。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-a952dc4e94a62588.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>之前写过百度<a href="https://mp.weixin.qq.com/s/7v9LHJX05Ybv39BzrMD0Uw" target="_blank" rel="noopener">那些你可能不知道的百度产品</a> 和腾讯<a href="https://mp.weixin.qq.com/s/3npDt-hSe97L987Z_DaWCQ" target="_blank" rel="noopener">那些你可能不知道的腾讯产品</a> ，正好再来说说谷歌，谷歌的所有产品 <a href="https://about.google/intl/zh-CN/products/" target="_blank" rel="noopener">https://about.google/intl/zh-CN/products/</a> 在这里有介绍，谷歌最好的产品自然是搜索了，可惜国内用不了（想上谷歌在公众号回复 <code>谷歌</code> 获取使用方法），尽管谷歌退出中国多年，但还是有不少服务能在国内继续使用，这里做个整理。</p>
<h3 id="Chrome-浏览器"><a href="#Chrome-浏览器" class="headerlink" title="Chrome 浏览器"></a>Chrome 浏览器</h3><p>我平常用的浏览器就是 Chrome 了，国内Chrome使用的是 google.cn域名（可惜不能用于搜索） <a href="https://www.google.cn/intl/zh-CN/chrome/" target="_blank" rel="noopener">https://www.google.cn/intl/zh-CN/chrome/</a> ，直接下载就好，强烈推荐你使用Chrome浏览器。</p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-60c811f238d8f1aa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="谷歌地图"><a href="#谷歌地图" class="headerlink" title="谷歌地图"></a>谷歌地图</h3><p>之前有域名 ditu.google.cn ，但现在上不去了 ，它会跳转到google.cn ，<a href="https://www.google.cn/maps" target="_blank" rel="noopener">https://www.google.cn/maps</a> 也会重定向到 google.cn ，但是电脑访问 <a href="http://www.google.cn//maps" target="_blank" rel="noopener">http://www.google.cn//maps</a> 居然还可以继续使用。</p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-402be51340e90914.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>还有个更酷炫的谷歌地球<a href="https://earth.google.com/web/" target="_blank" rel="noopener">https://earth.google.com/web/</a> ，可惜国内没法用。</p>
<h3 id="谷歌翻译"><a href="#谷歌翻译" class="headerlink" title="谷歌翻译"></a>谷歌翻译</h3><p><a href="http://translate.google.cn/" target="_blank" rel="noopener">http://translate.google.cn/</a> 这个二级域名可以直接使用谷歌翻译。<br> <img src="https://upload-images.jianshu.io/upload_images/17817191-327f26d4900e8298.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="谷歌开发者"><a href="#谷歌开发者" class="headerlink" title="谷歌开发者"></a>谷歌开发者</h3><p><a href="https://developers.google.cn/" target="_blank" rel="noopener">https://developers.google.cn/</a>  这是针对开发者使用的产品。<a href="https://developer.android.google.cn/?hl=th" target="_blank" rel="noopener">https://developer.android.google.cn/?hl=th</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-739cf61bf303eee6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>安卓开发者应该很需要 <a href="https://developer.android.google.cn/" target="_blank" rel="noopener">https://developer.android.google.cn/</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-13e2574811d26d78.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="golang"><a href="#golang" class="headerlink" title="golang"></a>golang</h3><p>golang 是谷歌开发的一种语言，<a href="https://golang.google.cn/" target="_blank" rel="noopener">https://golang.google.cn/</a> 。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-13e6f2a703bc6c51.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>文档也能打开<a href="https://golang.google.cn/doc/" target="_blank" rel="noopener">https://golang.google.cn/doc/</a> ，当然使用go get 的时候推荐使用代理 <a href="https://goproxy.io/" target="_blank" rel="noopener">https://goproxy.io/</a>  <a href="https://goproxy.cn/" target="_blank" rel="noopener">https://goproxy.cn/</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-34b6ea961fd1cee1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="谷歌广告"><a href="#谷歌广告" class="headerlink" title="谷歌广告"></a>谷歌广告</h3><p><a href="https://ads.google.cn/intl/zh-CN_cn/home/" target="_blank" rel="noopener">https://ads.google.cn/intl/zh-CN_cn/home/</a> 你应该经常能在网站上看到谷歌的广告，国内的公司也可以投放。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-4a1611cd35726121.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-6c2abff741600ab7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="谷歌文档"><a href="#谷歌文档" class="headerlink" title="谷歌文档"></a>谷歌文档</h3><p><a href="http://www.google.cn/docs/about/" target="_blank" rel="noopener">http://www.google.cn/docs/about/</a> 这个能打开，可惜点击后还是跳转到<a href="https://docs.google.com/document/?usp=mkt_docs" target="_blank" rel="noopener">https://docs.google.com/document/?usp=mkt_docs</a> ，对应的国内可以使用腾讯文档。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-d8ee2e08ae1fd7d5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="谷歌-DNS"><a href="#谷歌-DNS" class="headerlink" title="谷歌 DNS"></a>谷歌 DNS</h3><p>Google Public DNS 是Google于2009年12月5日起提供的一个免费域名解析服务，具体看维基百科<a href="https://zh.wikipedia.org/wiki/Google_Public_DNS" target="_blank" rel="noopener">https://zh.wikipedia.org/wiki/Google_Public_DNS</a><br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">λ ping <span class="number">8.8</span><span class="number">.8</span><span class="number">.8</span></span><br><span class="line"></span><br><span class="line">正在 Ping <span class="number">8.8</span><span class="number">.8</span><span class="number">.8</span> 具有 <span class="number">32</span> 字节的数据:</span><br><span class="line">来自 <span class="number">8.8</span><span class="number">.8</span><span class="number">.8</span> 的回复: 字节=<span class="number">32</span> 时间=<span class="number">41</span>ms TTL=<span class="number">52</span></span><br><span class="line">来自 <span class="number">8.8</span><span class="number">.8</span><span class="number">.8</span> 的回复: 字节=<span class="number">32</span> 时间=<span class="number">40</span>ms TTL=<span class="number">52</span></span><br><span class="line">来自 <span class="number">8.8</span><span class="number">.8</span><span class="number">.8</span> 的回复: 字节=<span class="number">32</span> 时间=<span class="number">42</span>ms TTL=<span class="number">52</span></span><br><span class="line">来自 <span class="number">8.8</span><span class="number">.8</span><span class="number">.8</span> 的回复: 字节=<span class="number">32</span> 时间=<span class="number">40</span>ms TTL=<span class="number">52</span></span><br><span class="line"></span><br><span class="line"><span class="number">8.8</span><span class="number">.8</span><span class="number">.8</span> 的 Ping 统计信息:</span><br><span class="line">    数据包: 已发送 = <span class="number">4</span>，已接收 = <span class="number">4</span>，丢失 = <span class="number">0</span> (<span class="number">0</span>% 丢失)，</span><br><span class="line">往返行程的估计时间(以毫秒为单位):</span><br><span class="line">    最短 = <span class="number">40</span>ms，最长 = <span class="number">42</span>ms，平均 = <span class="number">40</span>ms</span><br><span class="line"></span><br><span class="line">λ nslookup baidu.com <span class="number">8.8</span><span class="number">.8</span><span class="number">.8</span></span><br><span class="line">服务器:  dns.google</span><br><span class="line">Address:  <span class="number">8.8</span><span class="number">.8</span><span class="number">.8</span></span><br><span class="line"></span><br><span class="line">非权威应答:</span><br><span class="line">名称:    baidu.com</span><br><span class="line">Address:  <span class="number">39.156</span><span class="number">.69</span><span class="number">.79</span></span><br></pre></td></tr></table></figure></p>
<p>访问<code>39.156.69.79</code> 这个ip就可以直接访问百度。</p>
<p>另外谷歌学术 <a href="https://scholar.google.com/" target="_blank" rel="noopener">https://scholar.google.com/</a>  可以使用镜像 <a href="https://xs.glgoo.top/scholar/" target="_blank" rel="noopener">https://xs.glgoo.top/scholar/</a> ，搜索结果一样。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-111eefc50752e441.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>Google play 可以使用 <a href="https://apkcombo.com/apk-downloader/" target="_blank" rel="noopener">https://apkcombo.com/apk-downloader/</a> 输入play地址，比如谷歌拼音的<a href="https://play.google.com/store/apps/details?id=com.google.android.inputmethod.pinyin&amp;hl=zh_CN" target="_blank" rel="noopener">https://play.google.com/store/apps/details?id=com.google.android.inputmethod.pinyin&amp;hl=zh_CN</a> 获取apk下载地址安装到手机上，我试了下速度还可以。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-a0fcfc1e08762f07.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>我平常用得比较多就是 gmail ， 谷歌搜索，谷歌相册，相册可以借助 <a href="https://takeout.google.com/" target="_blank" rel="noopener">https://takeout.google.com/</a> 这个导出所有相片。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-af0c9272799fe896.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>除了上面这些产品，谷歌还陆陆续续关闭了不少产品，<a href="https://killedbygoogle.com/" target="_blank" rel="noopener">https://killedbygoogle.com/</a> 这个网站做了个整理，有上百个，我觉得Google+挺可惜的。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-348000439417080a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h3><p>Google地球<a href="https://www.google.com/intl/zh-CN/earth/" target="_blank" rel="noopener">https://www.google.com/intl/zh-CN/earth/</a><br>谷歌学术<a href="https://blog.csdn.net/love666666shen/article/details/75311407" target="_blank" rel="noopener">https://blog.csdn.net/love666666shen/article/details/75311407</a><br> <a href="https://www.scholarnet.cn/" target="_blank" rel="noopener">https://www.scholarnet.cn/</a><br>Google Fi 是Google旗下的移动虚拟运营商<a href="http://www.googlevoice.net/index.php/archives/125/" target="_blank" rel="noopener">http://www.googlevoice.net/index.php/archives/125/</a> <a href="https://fi.google.com/about/tos/" target="_blank" rel="noopener">https://fi.google.com/about/tos/</a><br><a href="https://cn.v2ex.com/t/606311" target="_blank" rel="noopener">https://cn.v2ex.com/t/606311</a><br>site:github.io 上谷歌   <a href="https://panmuboke.github.io/" target="_blank" rel="noopener">https://panmuboke.github.io/</a><br>谷歌学术<a href="https://lingan1996.github.io/ssr%E4%B8%8Agoogle-scholar/" target="_blank" rel="noopener">https://lingan1996.github.io/ssr%E4%B8%8Agoogle-scholar/</a><br><a href="https://haoel.github.io/" target="_blank" rel="noopener">https://haoel.github.io/</a><br>科学上网总结<a href="https://bibichuan.github.io/2019/02/16/%E7%A7%91%E5%AD%A6%E4%B8%8A%E7%BD%91%E7%9A%84%E6%8A%80%E6%9C%AF%E6%80%BB%E7%BB%93/" target="_blank" rel="noopener">https://bibichuan.github.io/2019/02/16/%E7%A7%91%E5%AD%A6%E4%B8%8A%E7%BD%91%E7%9A%84%E6%8A%80%E6%9C%AF%E6%80%BB%E7%BB%93/</a><br><a href="http://www.google.com/about" target="_blank" rel="noopener">http://www.google.com/about</a><br>谷歌声明<a href="https://www.google.com/press/shengming" target="_blank" rel="noopener">https://www.google.com/press/shengming</a><br><a href="https://lusongsong.com/reed/170.html" target="_blank" rel="noopener">https://lusongsong.com/reed/170.html</a><br><a href="http://xcx1024.com/ArtInfo/92473.html" target="_blank" rel="noopener">http://xcx1024.com/ArtInfo/92473.html</a><br><a href="https://www.google.cn/chrome/?standalone=1&amp;platform=win64" target="_blank" rel="noopener">https://www.google.cn/chrome/?standalone=1&amp;platform=win64</a><br><a href="http://www.google.cn/docs/about/" target="_blank" rel="noopener">http://www.google.cn/docs/about/</a><br><a href="https://www.zhihu.com/question/326911592" target="_blank" rel="noopener">https://www.zhihu.com/question/326911592</a><br>firebase.google.cn<br><a href="https://542869246.github.io/2018/10/23/%E6%8E%A8%E8%8D%90%E5%87%A0%E6%AC%BE%E4%B8%AA%E4%BA%BA%E4%BD%BF%E7%94%A8%E7%9A%84Chrome%E6%8F%92%E4%BB%B6/" target="_blank" rel="noopener">https://542869246.github.io/2018/10/23/%E6%8E%A8%E8%8D%90%E5%87%A0%E6%AC%BE%E4%B8%AA%E4%BA%BA%E4%BD%BF%E7%94%A8%E7%9A%84Chrome%E6%8F%92%E4%BB%B6/</a><br>chrome 扩展 <a href="https://wsgzao.github.io/post/chrome-extensions/" target="_blank" rel="noopener">https://wsgzao.github.io/post/chrome-extensions/</a><br><a href="https://asura88.github.io/2018/10/13/%E7%A7%91%E5%AD%A6%E4%B8%8A%E7%BD%91/" target="_blank" rel="noopener">https://asura88.github.io/2018/10/13/%E7%A7%91%E5%AD%A6%E4%B8%8A%E7%BD%91/</a><br>查看 Google 搜索结果中的缓存版网页 <a href="https://limbopro.github.io/archives/243.html" target="_blank" rel="noopener">https://limbopro.github.io/archives/243.html</a><br> <a href="https://github.com/ssrpanel/" target="_blank" rel="noopener">https://github.com/ssrpanel/</a><br>一个逗比写的逗比脚本 <a href="https://github.com/ToyoDAdoubi/doubi" target="_blank" rel="noopener">https://github.com/ToyoDAdoubi/doubi</a><br><a href="https://10beasts.net/best-vpn/china/" target="_blank" rel="noopener">https://10beasts.net/best-vpn/china/</a><br><a href="https://laod.cn/news/googles-21st-birthday.html" target="_blank" rel="noopener">https://laod.cn/news/googles-21st-birthday.html</a><br><a href="https://www.kanfeidie.com/idea/network/287.html" target="_blank" rel="noopener">https://www.kanfeidie.com/idea/network/287.html</a><br><a href="https://strivebo.com/2019/02/12/%E7%AF%871%EF%BC%9A%E7%A7%91%E5%AD%A6%E4%B8%8A%E7%BD%91%E6%96%B9%E5%BC%8F%E6%80%BB%E7%BB%93/" target="_blank" rel="noopener">https://strivebo.com/2019/02/12/%E7%AF%871%EF%BC%9A%E7%A7%91%E5%AD%A6%E4%B8%8A%E7%BD%91%E6%96%B9%E5%BC%8F%E6%80%BB%E7%BB%93/</a> 科学上网<br>墙外 <a href="https://www.bvsuper.com/index.php/archives/183/" target="_blank" rel="noopener">https://www.bvsuper.com/index.php/archives/183/</a><br>shadowsocks+vps+mac <a href="https://michael728.github.io/2019/04/27/tools-ss-vps/" target="_blank" rel="noopener">https://michael728.github.io/2019/04/27/tools-ss-vps/</a><br><a href="https://piaohanshenghuo.com/ke-xue-shang-wang/" target="_blank" rel="noopener">https://piaohanshenghuo.com/ke-xue-shang-wang/</a><br>google play apk <a href="https://www.anzhiapp.com/" target="_blank" rel="noopener">https://www.anzhiapp.com/</a><br>ssh脚本<a href="https://github.com/ToyoDAdoubi/doubi/" target="_blank" rel="noopener">https://github.com/ToyoDAdoubi/doubi/</a><br>手机谷歌 <a href="http://www.appchina.com/dna/538" target="_blank" rel="noopener">http://www.appchina.com/dna/538</a><br>google ws tls cdn</p>
<p>推荐阅读:</p>
<p><a href="https://mp.weixin.qq.com/s/iL6WyI-TChtjZMuu5G5W8A" target="_blank" rel="noopener">公众号苏生不惑原创文章整理</a></p>
<p><a href="https://mp.weixin.qq.com/s/Xz1m-mqtCcBF_4hmGCpkUQ" target="_blank" rel="noopener">如何发一条空白的朋友圈</a></p>
<p><a href="https://mp.weixin.qq.com/s/1omFkK5PPyeJEzUTagj9qg" target="_blank" rel="noopener">免费在线听周杰伦歌曲</a></p>
<p><a href="https://mp.weixin.qq.com/s/eGDO0Y8el_dsEyriCoAgog" target="_blank" rel="noopener">那些你可能不知道的微信奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/k5lBwiDzGgSU3fh2v2Rw9A" target="_blank" rel="noopener">如何在豆瓣租房小组快速找到满意的房子</a></p>
<p><a href="https://mp.weixin.qq.com/s/qNfuWjH54WHJtx4sEE5xwA" target="_blank" rel="noopener">10%+10% 不等于 0.2？</a></p>
<p><a href="https://mp.weixin.qq.com/s/adJFh_9LH0N-vvvYaiQqXg" target="_blank" rel="noopener">Chrome 浏览器扩展神器油猴</a></p>
<p><a href="https://mp.weixin.qq.com/s/HpuInXUCjSYT7HLqhoRcCA" target="_blank" rel="noopener">那些你可能不知道的 bilibili 奇技淫巧</a></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-393b26173c148690.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="免费星球"></p>
<h3 id="公众号：苏生不惑"><a href="#公众号：苏生不惑" class="headerlink" title="公众号：苏生不惑"></a>公众号：苏生不惑</h3><p> <img src="https://upload-images.jianshu.io/upload_images/17817191-6e0079f95d4c0338.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="扫描二维码关注"></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/15/那些你可能不知道的腾讯产品/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/10/15/那些你可能不知道的腾讯产品/" itemprop="url">那些你可能不知道的腾讯产品</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-10-15T20:32:29+08:00">
                2019-10-15
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.1k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  4 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>相比百度<a href="https://mp.weixin.qq.com/s/7v9LHJX05Ybv39BzrMD0Uw" target="_blank" rel="noopener">那些你可能不知道的百度产品</a>，腾讯的产品也是多如牛毛<a href="https://www.qq.com/map/" target="_blank" rel="noopener">https://www.qq.com/map/</a> 这里列出了腾讯几百个产品。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-de9865ae79d07025.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>QQ，微信，公众号这些就不说了，这里说下你可能不知道但非常实用的腾讯产品。</p>
<h3 id="腾讯文档"><a href="#腾讯文档" class="headerlink" title="腾讯文档"></a>腾讯文档</h3><p>看很多人在用石墨文档，其实腾讯文档也是一款可多人协作的在线文档，支持Word、Excel和PPT类型，打开网页就能查看和编辑，云端实时保存；可多人实时编辑文档，权限安全可控 。<a href="https://docs.qq.com/desktop" target="_blank" rel="noopener">https://docs.qq.com/desktop</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-d9a0812805d3c590.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>和本地使用Word体验差不多，markdown也支持，还支持多人同时编辑，手机端还有小程序，值得推荐下。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-b6c7960869e8ec57.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="微云"><a href="#微云" class="headerlink" title="微云"></a>微云</h3><p>微云是腾讯公司为用户精心打造的一项智能云服务, 您可以通过微云方便地在手机和电脑之间同步文件、推送照片和传输数据。<a href="https://www.weiyun.com/" target="_blank" rel="noopener">https://www.weiyun.com/</a> ，不知道为什么没用QQ的二级域名，除了百度网盘，微云也是个选择。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-94d19f7cf79080b9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>比如这个微博备份工具 <a href="https://share.weiyun.com/56v1cyU" target="_blank" rel="noopener">https://share.weiyun.com/56v1cyU</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-92a17af70ba52bf4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="吐个槽"><a href="#吐个槽" class="headerlink" title="吐个槽"></a>吐个槽</h3><p>吐个槽是腾讯推出的一款轻量、免费的用户意见反馈服务平台，旨在方便地嵌入APP/微信公众号/QQ公众号，为中小企业或团队快速搭建用户反馈通道，用户还能发帖反馈。 <a href="https://tucao.qq.com/" target="_blank" rel="noopener">https://tucao.qq.com</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-496d6b254ba5ccab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>如果你的公司想做一个用户反馈系统，完全可以用腾讯的这个，效果见<a href="https://support.qq.com/products/1368" target="_blank" rel="noopener">https://support.qq.com/products/1368</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-5cd8a552817c77ca.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="腾讯问卷"><a href="#腾讯问卷" class="headerlink" title="腾讯问卷"></a>腾讯问卷</h3><p>腾讯问卷，是腾讯公司推出的免费、专业的问卷调查系统。提供多种方式创建问卷，简单高效的编辑方式，强大的逻辑设置功能，专业的数据统计和样本甄别<a href="https://wj.qq.com/" target="_blank" rel="noopener">https://wj.qq.com/</a></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-a5d0cafb6af7f357.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="腾讯软件"><a href="#腾讯软件" class="headerlink" title="腾讯软件"></a>腾讯软件</h3><p>pc上下载软件不用百度挨个找，这里有非常多的软件 <a href="https://pc.qq.com/" target="_blank" rel="noopener">https://pc.qq.com/</a></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-47287afbdf3bde91.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>手机端也有，就是应用宝 <a href="https://sj.qq.com/" target="_blank" rel="noopener">https://sj.qq.com/</a></p>
<h3 id="图片处理"><a href="#图片处理" class="headerlink" title="图片处理"></a>图片处理</h3><p>有3款产品: 看图 <a href="https://kantu.qq.com/" target="_blank" rel="noopener">https://kantu.qq.com/</a> ， 截图 <a href="https://jietu.qq.com/，天天P图[https://tu.qq.com/](https://tu.qq.com/)，女生可能喜欢用。" target="_blank" rel="noopener">https://jietu.qq.com/，天天P图[https://tu.qq.com/](https://tu.qq.com/)，女生可能喜欢用。</a></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-6a10d2c328b9f8e5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="设计导航"><a href="#设计导航" class="headerlink" title="设计导航"></a>设计导航</h3><p>设计师应该知道这个导航站 <a href="https://idesign.qq.com/#!index/feed/id/1" target="_blank" rel="noopener">https://idesign.qq.com/#!index/feed/id/1</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-05cb083e8362fdbc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="腾讯智图"><a href="#腾讯智图" class="headerlink" title="腾讯智图"></a>腾讯智图</h3><p>智图是腾讯前端团队开发的一个专门用于图片压缩和图片格式转换的平台，可以对png,jpeg,gif等各类格式图片的压缩，以及为上传图片自动选择最优的图片格式，同时，智图平台还会为用户转换一份webp格式的图片，图片压缩后的质量非常好，而且体积减少很多。<a href="https://zhitu.isux.us/" target="_blank" rel="noopener">https://zhitu.isux.us/</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-1dbbe5ad7b943ddc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>还提供客户端<br><img src="https://upload-images.jianshu.io/upload_images/17817191-0d6620f340f99164.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>最后提下腾讯的小程序<code>灵鲲金融风险查询举报中心</code>，想知道如何判断一个项目是不是传销？这个小程序可以自动识别大部分传销项目。如果家里有人被忽悠加入了这样的项目，可以用来鉴别下。</p>
<p>还有哪些你常用的腾讯产品可以分享下。</p>
<h3 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h3><p>小Q机器人<a href="https://qrobot.qq.com/#/" target="_blank" rel="noopener">https://qrobot.qq.com/#/</a><br>微信电话本<a href="https://dianhua.qq.com/" target="_blank" rel="noopener">https://dianhua.qq.com</a><br><a href="https://ixdc.org/?p=14274" target="_blank" rel="noopener">https://ixdc.org/?p=14274</a><br><a href="https://ai.qq.com" target="_blank" rel="noopener">https://ai.qq.com</a> 手机号即可注册，不限调用次数，默认给 1QPS 的并发；<br>填写姓名、身份证和银行卡号后升级到 2QPS<br><a href="https://www.yuque.com/dashboard" target="_blank" rel="noopener">https://www.yuque.com/dashboard</a><br>Office 365 是一种订阅式的跨平台办公软件，基于云平台提供多种服务，通过将 Excel 和 Outlook 等应用与 OneDrive 和 Microsoft Teams 等强大的云服务相结合，Office 365 可让任何人使用任何设备随时随地创建和共享内容。可支持多人协同在线编辑 。<a href="https://kimgkw2-my.sharepoint.com/:w:/g/personal/llpdev_52ove_cf/EUjFI3YILWJPjTXUbtyX-fQB1J1TjmzfVhMMrt2atP1-7A?e=JEaHBC" target="_blank" rel="noopener">https://kimgkw2-my.sharepoint.com/:w:/g/personal/llpdev_52ove_cf/EUjFI3YILWJPjTXUbtyX-fQB1J1TjmzfVhMMrt2atP1-7A?e=JEaHBC</a><br>推荐阅读:</p>
<p><a href="https://mp.weixin.qq.com/s/Xz1m-mqtCcBF_4hmGCpkUQ" target="_blank" rel="noopener">如何发一条空白的朋友圈</a></p>
<p><a href="https://mp.weixin.qq.com/s/1omFkK5PPyeJEzUTagj9qg" target="_blank" rel="noopener">免费在线听周杰伦歌曲</a></p>
<p><a href="https://mp.weixin.qq.com/s/eGDO0Y8el_dsEyriCoAgog" target="_blank" rel="noopener">那些你可能不知道的微信奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/k5lBwiDzGgSU3fh2v2Rw9A" target="_blank" rel="noopener">如何在豆瓣租房小组快速找到满意的房子</a></p>
<p><a href="https://mp.weixin.qq.com/s/qNfuWjH54WHJtx4sEE5xwA" target="_blank" rel="noopener">10%+10% 不等于 0.2？</a></p>
<p><a href="https://mp.weixin.qq.com/s/adJFh_9LH0N-vvvYaiQqXg" target="_blank" rel="noopener">Chrome 浏览器扩展神器油猴</a></p>
<p><a href="https://mp.weixin.qq.com/s/HpuInXUCjSYT7HLqhoRcCA" target="_blank" rel="noopener">那些你可能不知道的 bilibili 奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/tPvrcgYb3KTlr-jyO3hPMg" target="_blank" rel="noopener">上班工作压力太大?来玩个游戏解压下</a></p>
<p><a href="https://mp.weixin.qq.com/s/7v9LHJX05Ybv39BzrMD0Uw" target="_blank" rel="noopener">那些你可能不知道的百度产品</a></p>
<h3 id="公众号：苏生不惑"><a href="#公众号：苏生不惑" class="headerlink" title="公众号：苏生不惑"></a>公众号：苏生不惑</h3><p> <img src="https://upload-images.jianshu.io/upload_images/17817191-6e0079f95d4c0338.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="扫描二维码关注"></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/12/php-单元测试/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/10/12/php-单元测试/" itemprop="url">php 单元测试</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-10-12T16:03:31+08:00">
                2019-10-12
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  831 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  4 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="常用命令"><a href="#常用命令" class="headerlink" title="常用命令"></a>常用命令</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">新建一个单元测试</span><br><span class="line"></span><br><span class="line">php artisan make:test ReplyTest --unit</span><br><span class="line">单独执行某个测试</span><br><span class="line"></span><br><span class="line"> phpunit tests/Unit/ReplyTest.php</span><br><span class="line">单独执行某个测试中的函数</span><br><span class="line"></span><br><span class="line">phpunit --filter a_thread_has_a_creator</span><br><span class="line">带上测试环境变量进行测试就不需要 CsrfToken</span><br><span class="line"></span><br><span class="line">APP_ENV=testing phpunit --filter an_authenticated_user_may_participate_in_forum_threads</span><br><span class="line"></span><br><span class="line">APP_ENV=testing phpunit</span><br></pre></td></tr></table></figure>
<h3 id="测试类例子"><a href="#测试类例子" class="headerlink" title="测试类例子"></a>测试类例子</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TestCase</span> <span class="keyword">extends</span> <span class="title">Illuminate</span>\<span class="title">Foundation</span>\<span class="title">Testing</span>\<span class="title">TestCase</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Creates the application.</span></span><br><span class="line"><span class="comment">	 *</span></span><br><span class="line"><span class="comment">	 * @return \Illuminate\Foundation\Application</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	public <span class="function"><span class="keyword">function</span> <span class="title">createApplication</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		$app = <span class="built_in">require</span> __DIR__.<span class="string">'/../bootstrap/app.php'</span>;</span><br><span class="line"></span><br><span class="line">		$app-&gt;make(<span class="string">'Illuminate\Contracts\Console\Kernel'</span>)-&gt;bootstrap();</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> $app;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">FooTest</span> <span class="keyword">extends</span> <span class="title">TestCase</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">testSomethingIsTrue</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertTrue(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">你可以从终端机执行phpunit 命令来执行应用程序的所有测试。</span><br><span class="line"></span><br><span class="line">$response = $<span class="keyword">this</span>-&gt;call(<span class="string">'GET'</span>, <span class="string">'user/profile'</span>);</span><br><span class="line"></span><br><span class="line">$response = $<span class="keyword">this</span>-&gt;call($method, $uri, $parameters,  $cookies, $files, $server, $content);</span><br><span class="line">接着你可以检查Illuminate\Http\Response 对象：</span><br><span class="line"></span><br><span class="line">$<span class="keyword">this</span>-&gt;assertEquals(<span class="string">'Hello World'</span>, $response-&gt;getContent());</span><br><span class="line">从测试调用控制器</span><br><span class="line"></span><br><span class="line">你也可以从测试调用控制器：</span><br><span class="line"></span><br><span class="line">$response = $<span class="keyword">this</span>-&gt;action(<span class="string">'GET'</span>, <span class="string">'HomeController@index'</span>);</span><br><span class="line"></span><br><span class="line">$response = $<span class="keyword">this</span>-&gt;action(<span class="string">'GET'</span>, <span class="string">'UserController@profile'</span>, array(<span class="string">'user'</span> =&gt; <span class="number">1</span>));</span><br></pre></td></tr></table></figure>
<h3 id="phpunit-xml"><a href="#phpunit-xml" class="headerlink" title="phpunit.xml"></a>phpunit.xml</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=<span class="string">"1.0"</span> encoding=<span class="string">"UTF-8"</span>?&gt;</span><br><span class="line">&lt;phpunit backupGlobals=<span class="string">"false"</span></span><br><span class="line">         backupStaticAttributes=<span class="string">"false"</span></span><br><span class="line">         bootstrap=<span class="string">"bootstrap/autoload.php"</span></span><br><span class="line">         colors=<span class="string">"true"</span></span><br><span class="line">         convertErrorsToExceptions=<span class="string">"true"</span></span><br><span class="line">         convertNoticesToExceptions=<span class="string">"true"</span></span><br><span class="line">         convertWarningsToExceptions=<span class="string">"true"</span></span><br><span class="line">         processIsolation=<span class="string">"false"</span></span><br><span class="line">         stopOnFailure=<span class="string">"false"</span></span><br><span class="line">         syntaxCheck=<span class="string">"false"</span>&gt;</span><br><span class="line">    &lt;testsuites&gt;</span><br><span class="line">        &lt;testsuite name=<span class="string">"Application Test Suite"</span>&gt;</span><br><span class="line">            &lt;directory&gt;./tests/&lt;/directory&gt;</span><br><span class="line">        &lt;<span class="regexp">/testsuite&gt;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>testsuites&gt;</span><br><span class="line">    &lt;php&gt;</span><br><span class="line">        &lt;env name=<span class="string">"APP_ENV"</span> value=<span class="string">"testing"</span>/&gt;</span><br><span class="line">        &lt;env name=<span class="string">"CACHE_DRIVER"</span> value=<span class="string">"array"</span>/&gt;</span><br><span class="line">        &lt;env name=<span class="string">"SESSION_DRIVER"</span> value=<span class="string">"array"</span>/&gt;</span><br><span class="line">        &lt;env name=<span class="string">"QUEUE_DRIVER"</span> value=<span class="string">"sync"</span>/&gt;</span><br><span class="line">    &lt;<span class="regexp">/php&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>phpunit&gt;</span><br></pre></td></tr></table></figure>
<h3 id="覆盖率"><a href="#覆盖率" class="headerlink" title="覆盖率"></a>覆盖率</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">pecl install xdebug</span><br><span class="line">php.ini 手动添加 zend_extension=xdebug.so</span><br><span class="line"> php -m|grep xdebug</span><br><span class="line"></span><br><span class="line">phpunit --coverage-html ./tests/report 来生成 html 报告</span><br><span class="line"></span><br><span class="line">在 phpunit.xml 添加如下代码：</span><br><span class="line">&lt;filter&gt;</span><br><span class="line">        &lt;!--这里配置了白名单，只有这里边的代码会被统计覆盖率--&gt;</span><br><span class="line">        &lt;whitelist processUncoveredFilesFromWhitelist=<span class="string">"true"</span>&gt;</span><br><span class="line">            &lt;directory suffix=<span class="string">".php"</span>&gt;./tests/Unit&lt;<span class="regexp">/directory&gt;</span></span><br><span class="line"><span class="regexp">        &lt;/</span>whitelist&gt;</span><br><span class="line">    &lt;<span class="regexp">/filter&gt;</span></span><br><span class="line"><span class="regexp">&lt;logging&gt;</span></span><br><span class="line"><span class="regexp">   &lt;log type="coverage-html" target="./</span>tests/report<span class="string">" charset="</span>UTF<span class="number">-8</span><span class="string">"/&gt;</span></span><br><span class="line"><span class="string">&lt;/logging&gt;</span></span><br><span class="line"><span class="string"> 然后直接执行 phpunit 即可。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> https://learnku.com/articles/24030</span></span><br><span class="line"><span class="string">视频 https://58hualong.com/course/16/PHP-dan-yuan-ce-shi</span></span><br><span class="line"><span class="string">第七课：Mockery</span></span><br></pre></td></tr></table></figure>
<h3 id="测试demo"><a href="#测试demo" class="headerlink" title="测试demo"></a>测试demo</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">git clone https:<span class="comment">//github.com/sebastianbergmann/money.git</span></span><br><span class="line">phpunit --configuration build/phpunit.xml</span><br><span class="line"><span class="comment">//或者如下执行方式也行</span></span><br><span class="line">php build/tools/phpunit.phar --configuration build/phpunit.xml</span><br><span class="line">http:<span class="comment">//yyeer.com/%E7%BC%96%E7%A8%8B/2018/05/04/PHP%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%E4%B8%80%E8%B5%B7%E7%8E%A9/#4phpunit指令和xml配置了解一下</span></span><br></pre></td></tr></table></figure>
<h3 id="hook"><a href="#hook" class="headerlink" title="hook"></a>hook</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">PHPUnit则为我们提供了全面的hook接口：</span><br><span class="line"></span><br><span class="line">public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">setUpBeforeClass</span>(<span class="params"></span>)/<span class="title">tearDownAfterClass</span>(<span class="params"></span>)//测试类构建/解构时调用</span></span><br><span class="line"><span class="function"><span class="title">protected</span> <span class="title">function</span> <span class="title">setUp</span>(<span class="params"></span>)/<span class="title">tearDown</span>(<span class="params"></span>)//测试方法执行前/后调用</span></span><br><span class="line"><span class="function"><span class="title">protected</span> <span class="title">function</span> <span class="title">assertPreConditions</span>(<span class="params"></span>)/<span class="title">assertPostConditions</span>(<span class="params"></span>)//断言前/后调用</span></span><br><span class="line"><span class="function">&lt;?<span class="title">php</span></span></span><br><span class="line"><span class="function"><span class="title">namespace</span> <span class="title">Tests</span>;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">use</span> <span class="title">App</span>\<span class="title">Example</span>;</span></span><br><span class="line"><span class="function"><span class="title">use</span> <span class="title">PHPUnit</span>\<span class="title">Framework</span>\<span class="title">TestCase</span> <span class="title">as</span> <span class="title">BaseTestCase</span>;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">class</span> <span class="title">ExampleTest</span> <span class="title">extends</span> <span class="title">BaseTestCase</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 类静态属性</span></span><br><span class="line">    private <span class="keyword">static</span> $example;</span><br><span class="line"></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">setUpBeforeClass</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        self::$example = <span class="keyword">new</span> Example();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">testGetTrue</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 类的静态属性更新</span></span><br><span class="line">        self::$example-&gt;setMsg(<span class="string">"hello big_cat"</span>);</span><br><span class="line">        $result = self::$example-&gt;getTrue();</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertTrue($result);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">testGetFalse</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $result = self::$example-&gt;getFalse();</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertFalse($result);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 依赖 testGetTrue 执行完毕</span></span><br><span class="line"><span class="comment">     * @depends testGetTrue</span></span><br><span class="line"><span class="comment">     * @return [type] [description]</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">testGetMsg</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $result = self::$example-&gt;getMsg();</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertEquals($result, <span class="string">"hello big_cat"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><a href="https://xueyuanjun.com/post/19576.html" target="_blank" rel="noopener">Laravel 中基于 PHPUnit 进行代码测试</a></p>
<p><a href="https://github.com/rovast/phpunit-demo" target="_blank" rel="noopener">单元测试demo</a></p>
<p><a href="https://learnku.com/articles/17529" target="_blank" rel="noopener">Laravel 单元测试那些事</a></p>
<p><a href="https://wiki.jikexueyuan.com/project/laravel/testing.html" target="_blank" rel="noopener">单元测试</a></p>
<p><a href="https://iyaozhen.com/php-unit-test-mock-and-db-test.html" target="_blank" rel="noopener">PHP单元测试-mock和数据库测试</a></p>
<p><a href="https://learnku.com/docs/laravel/5.8/testing/3937" target="_blank" rel="noopener">测试：入门指南</a></p>
<p><a href="http://0x3f.org/post/unit-testing-standard-for-laravel-based-projects/" target="_blank" rel="noopener">基于Laravel的项目的单元测试规范</a></p>
<p><a href="https://phpunit.readthedocs.io/zh_CN/latest/test-doubles.html" target="_blank" rel="noopener">phpunit文档</a></p>
<p><a href="https://learnku.com/laravel/t/22814" target="_blank" rel="noopener">PHPUnit 入门教程</a></p>
<p><a href="https://learnku.com/articles/24030" target="_blank" rel="noopener">PHP 单元测试覆盖率</a></p>
<p><a href="https://learnku.com/articles/22072" target="_blank" rel="noopener">phpunit 快速入门</a></p>
<p><a href="https://learnku.com/docs/phpunit-testing-laravel/1introduction-to-application-testing/2467" target="_blank" rel="noopener">测试简介</a></p>
<p><a href="https://www.jianshu.com/p/1c929f7fe7ed" target="_blank" rel="noopener">使用 PHPUnit 进行单元和功能测试 </a></p>
<p><a href="https://learnku.com/laravel/t/22814" target="_blank" rel="noopener">Laravel 测试之 —— PHPUnit 入门教程</a></p>
<p><a href="https://segmentfault.com/a/1190000018426487" target="_blank" rel="noopener">PHPUnit 进行单元测试并生成代码覆盖率报告</a></p>
<p><a href="https://segmentfault.com/a/1190000010605518" target="_blank" rel="noopener">使用桩件 (Stub) 解决 Laravel 单元测试中的依赖</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/09/公众号苏生不惑原创文章整理/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/10/09/公众号苏生不惑原创文章整理/" itemprop="url">公众号苏生不惑原创文章整理</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-10-09T20:13:51+08:00">
                2019-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.1k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  3 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>过去几个月公众号坚持每周更新，一周不拉，我都佩服自己坚持这么久，不知不觉更新了近50篇原创文章，由于平常要上班，所以一般都是周末写文章（还好新浪没有996），其实写完一篇文章挺费时的，我也不写无聊的口水文，每篇文章都要整理，测试，校对，经常写到凌晨一点。</p>
<p>还好这个号关注人数在不断增加，星球也450+人了，有兴趣免费加入我的星球，几乎每天更新一些有意思的内容，我几乎把它当微博用。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-393b26173c148690.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="免费星球"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-d695dfb2a13f74e7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-9aba38d027084702.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-85c4549ffc9e8621.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>文章多了，翻历史记录也不方便，所以这里做个整理，方便大家查看。</p>
<h3 id="奇技淫巧"><a href="#奇技淫巧" class="headerlink" title="奇技淫巧"></a>奇技淫巧</h3><p>这个系列写了9篇了，之后还会更新。</p>
<p><a href="https://mp.weixin.qq.com/s/-cSjrvkibYGp5Fx8gCTFuw" target="_blank" rel="noopener">那些你可能不知道的浏览器奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/sqRgMh4rxFBt5YxNtaa6dw" target="_blank" rel="noopener">那些你可能不知道的知乎奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/eGDO0Y8el_dsEyriCoAgog" target="_blank" rel="noopener">那些你可能不知道的微信奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/j7VhoZXmUTnOWC5C_B8jlQ" target="_blank" rel="noopener">那些你可能不知道的微博奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/LtI2piwAIDXA590NEsXvuw" target="_blank" rel="noopener">那些你可能不知道的网易云音乐奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/-5tZWfeWWa_E8jRCH0T_Cw" target="_blank" rel="noopener">那些你可能不知道的搜索奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/5InOxKmi9eXk33G_8N3byQ" target="_blank" rel="noopener">那些你可能不知道的视频下载奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/R3x-xZwqLIVwPjlgikDQ9A" target="_blank" rel="noopener">那些你可能不知道的免费观看 VIP 视频奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/HpuInXUCjSYT7HLqhoRcCA" target="_blank" rel="noopener">那些你可能不知道的 bilibili 奇技淫巧</a></p>
<h3 id="微信"><a href="#微信" class="headerlink" title="微信"></a>微信</h3><p>这个系列是关于微信的一些实用技巧。</p>
<p><a href="https://mp.weixin.qq.com/s/eGDO0Y8el_dsEyriCoAgog" target="_blank" rel="noopener">那些你可能不知道的微信奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/GK3fp-cUSeByUrqINGFXHg" target="_blank" rel="noopener">微信好友头像拼图</a></p>
<p><a href="https://mp.weixin.qq.com/s/Xz1m-mqtCcBF_4hmGCpkUQ" target="_blank" rel="noopener">如何发一条空白的朋友圈</a></p>
<p><a href="https://mp.weixin.qq.com/s/_3AeNahwbs8c3UJ0is1t4A" target="_blank" rel="noopener">如何在电脑上登陆多个微信</a></p>
<p><a href="https://mp.weixin.qq.com/s/ekDfBG33Ko_BHXyeghq-Cw" target="_blank" rel="noopener">朋友圈点赞生成器</a></p>
<p><a href="https://mp.weixin.qq.com/s/TuJIqv5wv27avFKG8zmJUQ" target="_blank" rel="noopener">我是如何用 Markdown 写公众号文章的</a></p>
<p><a href="https://mp.weixin.qq.com/s/l2bQJk1qjb6IzroODBpoOg" target="_blank" rel="noopener">想看的公众号文章被删了怎么办？</a></p>
<p><a href="https://mp.weixin.qq.com/s/iL6WyI-TChtjZMuu5G5W8A" target="_blank" rel="noopener">公众号苏生不惑原创文章整理</a></p>
<h3 id="Python"><a href="#Python" class="headerlink" title="Python"></a>Python</h3><p>Python是个很好的脚本语言，用来处理一些日常工作还是很方便的。</p>
<p><a href="https://mp.weixin.qq.com/s/T1aAhBmFGdWjHMxpTwWcdg" target="_blank" rel="noopener">Python 文档生成器 mkdocs </a></p>
<p><a href="https://mp.weixin.qq.com/s/9VmirnT2W6I2x8mvzUeIEw" target="_blank" rel="noopener">比 man 更强悍的命令行工具 cheat</a></p>
<p><a href="https://mp.weixin.qq.com/s/624Hv1krGUboecz1QX8O1g" target="_blank" rel="noopener">ffmpeg 将视频转GIF</a></p>
<p><a href="https://mp.weixin.qq.com/s/-yta6BOJPfmLs9WVzFulyw" target="_blank" rel="noopener">5 分钟使用 hugo 搭建一个自己的博客</a></p>
<p><a href="https://mp.weixin.qq.com/s/TrKg_21PXVinpcqJIu73Ug" target="_blank" rel="noopener">命令行录制工具 asciinema</a></p>
<p><a href="https://mp.weixin.qq.com/s/WNuRKxtvwK_6AAnKmNU8yw" target="_blank" rel="noopener">ppt 神器 reveal</a></p>
<p><a href="https://mp.weixin.qq.com/s/-l7CnvU6Yu3CIcLizPJtYg" target="_blank" rel="noopener">Pyhon 爬虫框架 looter</a></p>
<p><a href="https://mp.weixin.qq.com/s/ly4KnTCvBLlyqFBbBleFCQ" target="_blank" rel="noopener">Python 抓取知识星球内容生成词云和 PDF</a></p>
<p><a href="https://mp.weixin.qq.com/s?__biz=MzIyMjg2ODExMA==&amp;mid=2247484060&amp;idx=1&amp;sn=81496dd7741195d89cf124916a81c74e&amp;chksm=e827a57ddf502c6b45f9a208b23f6b64e7058f741abbd2e97661c18548d09ab70be090e2e0e7&amp;token=1056434362&amp;lang=zh_CN#rd" target="_blank" rel="noopener">各种数据库命令行工具 mycli litecli mssql-cli pgcli</a></p>
<h3 id="工具"><a href="#工具" class="headerlink" title="工具"></a>工具</h3><p>工欲善其事，必先利其器，这些工具能更好的帮助你。</p>
<p><a href="https://mp.weixin.qq.com/s/9XEABuraTAipZ_uZPDbQdQ" target="_blank" rel="noopener">那些我常用的 Chrome 扩展</a></p>
<p><a href="https://mp.weixin.qq.com/s/P2BTgqGwsA4YAq4jve-2Cg" target="_blank" rel="noopener">那些除百度以外你可能不知道的小众搜索引擎</a></p>
<p><a href="https://mp.weixin.qq.com/s/CHb3gOMfGjwNjaznbv0R4g" target="_blank" rel="noopener">那些你可能不知道的 ocr 图片文字识别工具</a></p>
<p><a href="https://mp.weixin.qq.com/s/-ycONLAF7n-Xmvv4-NN-cg" target="_blank" rel="noopener">那些你可能不知道的 PDF 工具</a></p>
<p><a href="https://mp.weixin.qq.com/s/sng3uK9Nge1OD2gc5DDuZg" target="_blank" rel="noopener">那些你可能用得上的简历写作工具</a></p>
<p><a href="https://mp.weixin.qq.com/s/hbzmqep2JMICL6WogE9TCQ" target="_blank" rel="noopener">有意思的举牌告白小人</a></p>
<p><a href="https://mp.weixin.qq.com/s/adJFh_9LH0N-vvvYaiQqXg" target="_blank" rel="noopener">Chrome 浏览器扩展神器油猴</a></p>
<p><a href="https://mp.weixin.qq.com/s/H8VPSGt62-tblpgWkleQoQ" target="_blank" rel="noopener">那些你可能不知道的百度文库/网盘快速下载工具</a></p>
<p><a href="https://mp.weixin.qq.com/s/iluJYFYgT8bpMhrsxf0b5A" target="_blank" rel="noopener">Chrome 浏览器扩展神器暴力猴</a></p>
<p><a href="https://mp.weixin.qq.com/s/Ebrhd0nRucEJqxOzKVM_0g" target="_blank" rel="noopener">那些你可能不知道的文件分享工具</a></p>
<p><a href="https://mp.weixin.qq.com/s/MUIPj4OgSxAeRl5Hk-2tuw" target="_blank" rel="noopener">没有提取码怎么获取百度网盘资源？</a></p>
<p><a href="https://mp.weixin.qq.com/s/DzbDwX65Eb8beBzKun16wg" target="_blank" rel="noopener">怕被发垃圾邮件？用临时邮箱来注册账号</a></p>
<p><a href="https://mp.weixin.qq.com/s/FStz_y7oHa9KHrjkpqnsXw" target="_blank" rel="noopener">如何下载优酷 m3u8 格式的视频文件？</a></p>
<h3 id="工作生活"><a href="#工作生活" class="headerlink" title="工作生活"></a>工作生活</h3><p>工作压力太大，偶尔摸鱼娱乐下是有必要的。</p>
<p><a href="https://mp.weixin.qq.com/s/7v9LHJX05Ybv39BzrMD0Uw" target="_blank" rel="noopener">那些你可能不知道的百度产品</a></p>
<p><a href="https://mp.weixin.qq.com/s/tPvrcgYb3KTlr-jyO3hPMg" target="_blank" rel="noopener">上班工作压力太大?来玩个游戏解压下</a></p>
<p><a href="https://mp.weixin.qq.com/s/y8BrtwH4vlXlHKWQ9OMhdw" target="_blank" rel="noopener">听说你想 520 表白</a></p>
<p><a href="https://mp.weixin.qq.com/s/qyFvOgHf1mXwPKO0tQwUyg" target="_blank" rel="noopener">如何提取公积金 9 天到账</a></p>
<p><a href="https://mp.weixin.qq.com/s/1omFkK5PPyeJEzUTagj9qg" target="_blank" rel="noopener">免费在线听周杰伦歌曲</a></p>
<p><a href="https://mp.weixin.qq.com/s/k5lBwiDzGgSU3fh2v2Rw9A" target="_blank" rel="noopener">如何在豆瓣租房小组快速找到满意的房子</a></p>
<p><a href="https://mp.weixin.qq.com/s/qNfuWjH54WHJtx4sEE5xwA" target="_blank" rel="noopener">10%+10% 不等于 0.2？</a></p>
<p><a href="https://mp.weixin.qq.com/s/mI5kubVY2t5jwJ9ub7A1iA" target="_blank" rel="noopener">我的新浪工作日常</a></p>
<p><a href="https://mp.weixin.qq.com/s/MOSo-SHTz1zaJaXQM3tnqg" target="_blank" rel="noopener">上班摸鱼好去处</a></p>
<p><a href="https://mp.weixin.qq.com/s/MsK-yZfYP0Kwjtg_f4GWZQ" target="_blank" rel="noopener">历史上消失的那 10 天去哪了？ </a></p>
<p><a href="https://mp.weixin.qq.com/s?__biz=MzIyMjg2ODExMA==&amp;mid=2247484102&amp;idx=1&amp;sn=174675908e7ff4d9137197a52cb75526&amp;chksm=e827a527df502c3184ba6f13e5c4053709459ad5e42e1f1b429c93e63b1856f70d7d08daa719&amp;token=1056434362&amp;lang=zh_CN#rd" target="_blank" rel="noopener">表白新姿势</a></p>
<p>其中 阅读量最高分享最多的文章是这篇 <a href="https://mp.weixin.qq.com/s/Xz1m-mqtCcBF_4hmGCpkUQ" target="_blank" rel="noopener">如何发一条空白的朋友圈</a>，你也来发条空白朋友圈装逼下吧。</p>
<p>其次就是<br><a href="https://mp.weixin.qq.com/s/HpuInXUCjSYT7HLqhoRcCA" target="_blank" rel="noopener">那些你可能不知道的 bilibili 奇技淫巧</a>  ，<br><a href="https://mp.weixin.qq.com/s/k5lBwiDzGgSU3fh2v2Rw9A" target="_blank" rel="noopener">如何在豆瓣租房小组快速找到满意的房子</a> ，<br><a href="https://mp.weixin.qq.com/s/5InOxKmi9eXk33G_8N3byQ" target="_blank" rel="noopener">那些你可能不知道的视频下载奇技淫巧</a><br> 这几篇。</p>
<h3 id="公众号词云"><a href="#公众号词云" class="headerlink" title="公众号词云"></a>公众号词云</h3><p>写了这么多文章，来看看我的文章都涉及写哪些内容，这里用新榜的词云工具，输入公众号名称就行。<a href="https://data.newrank.cn/wordCloud.html" target="_blank" rel="noopener">https://data.newrank.cn/wordCloud.html</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-33a1347788d43fd8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>效果如图，看来我写百度，奇技淫巧最多。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-e8f241d143ad0898.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="公众号：苏生不惑"><a href="#公众号：苏生不惑" class="headerlink" title="公众号：苏生不惑"></a>公众号：苏生不惑</h3><p> <img src="https://upload-images.jianshu.io/upload_images/17817191-6e0079f95d4c0338.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="扫描二维码关注"></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/09/那些我常用的-Chrome-扩展续/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/10/09/那些我常用的-Chrome-扩展续/" itemprop="url">那些我常用的 Chrome 扩展续</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-10-09T20:13:11+08:00">
                2019-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.6k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  6 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>之前分享过<a href="https://mp.weixin.qq.com/s/9XEABuraTAipZ_uZPDbQdQ" target="_blank" rel="noopener">那些我常用的 Chrome 扩展</a> ，一篇文章内容有限，于是有了后续篇。</p>
<h3 id="微贝"><a href="#微贝" class="headerlink" title="微贝"></a>微贝</h3><p>这是一个微博备份的扩展，由于没有上架，只能下载到本地安装，下载地址<br><a href="https://share.weiyun.com/56v1cyU" target="_blank" rel="noopener">https://share.weiyun.com/56v1cyU</a>，具体使用见微博<a href="https://weibo.com/1088413295/I7MAriyVV" target="_blank" rel="noopener">https://weibo.com/1088413295/I7MAriyVV</a> ，需要用微信扫码登录。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-f938b2a9a1b7a38e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="微信同步助手"><a href="#微信同步助手" class="headerlink" title="微信同步助手"></a>微信同步助手</h3><p>微信公众号文章多平台同步，支持今日头条、WordPress、知乎、简书、typecho各大平台，一次发布，多平台同步发布。解放个人生产力，不用每个平台都复制一遍发布。</p>
<p>比如这篇文章 <a href="https://mp.weixin.qq.com/s/HpuInXUCjSYT7HLqhoRcCA" target="_blank" rel="noopener">那些你可能不知道的 bilibili 奇技淫巧</a>，先登录知乎和简书，然后直接同步到简书和知乎。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-ba4258ff52289fd7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>Chrome扩展地址 <a href="https://chrome.google.com/webstore/detail/%E5%BE%AE%E4%BF%A1%E5%90%8C%E6%AD%A5%E5%8A%A9%E6%89%8B/hchobocdmclopcbnibdnoafilagadion?hl=zh-CN" target="_blank" rel="noopener">https://chrome.google.com/webstore/detail/%E5%BE%AE%E4%BF%A1%E5%90%8C%E6%AD%A5%E5%8A%A9%E6%89%8B/hchobocdmclopcbnibdnoafilagadion?hl=zh-CN</a></p>
<h3 id="Easy-to-RSS"><a href="#Easy-to-RSS" class="headerlink" title="Easy to RSS"></a>Easy to RSS</h3><p>很多网站都有rss，可以让用户获取网站内容的最新更新。这个扩展可以直接获取网页RSS，复制到剪贴板并打开该RSS网页，并且与RSSHub联动。</p>
<p>比如b站up主 电影talks 的主页<a href="https://space.bilibili.com/319751089/" target="_blank" rel="noopener">https://space.bilibili.com/319751089/</a>，点击右侧rss可以看到获取动态，专栏文章等。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-ecb1d37d3123511f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>用户动态rss 链接 <a href="https://rsshub.app/bilibili/user/dynamic/319751089" target="_blank" rel="noopener">https://rsshub.app/bilibili/user/dynamic/319751089</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-1e23d79b714dcdb0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>Chrome扩展地址 <a href="https://chrome.google.com/webstore/detail/easy-to-rss/hbcmpkcpbnecinpngdnfbnknfkdpdfli" target="_blank" rel="noopener">https://chrome.google.com/webstore/detail/easy-to-rss/hbcmpkcpbnecinpngdnfbnknfkdpdfli</a></p>
<h3 id="哔哩哔哩助手"><a href="#哔哩哔哩助手" class="headerlink" title="哔哩哔哩助手"></a>哔哩哔哩助手</h3><p>这是一个b站 辅助工具 <a href="https://bilibili-helper.github.io/" target="_blank" rel="noopener">https://bilibili-helper.github.io/</a> ，可以替换播放器、去广告、推送通知并进行一些快捷操作。</p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-e2c8e8a103cf26fd?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image" title="image.png"></p>
<p>比如这个视频 <a href="https://www.bilibili.com/video/av10914119" target="_blank" rel="noopener">https://www.bilibili.com/video/av10914119</a> ，安装扩展登录b站后右侧有个助手按钮，可以直接下载当前播放的视频和弹幕。</p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-fe3494a814b10945?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image"><br>Chrome扩展地址 <a href="https://chrome.google.com/webstore/detail/%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9%E5%8A%A9%E6%89%8B%EF%BC%9Abilibilicom-%E7%BB%BC%E5%90%88%E8%BE%85%E5%8A%A9%E6%89%A9%E5%B1%95/kpbnombpnpcffllnianjibmpadjolanh" target="_blank" rel="noopener">https://chrome.google.com/webstore/detail/%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9%E5%8A%A9%E6%89%8B%EF%BC%9Abilibilicom-%E7%BB%BC%E5%90%88%E8%BE%85%E5%8A%A9%E6%89%A9%E5%B1%95/kpbnombpnpcffllnianjibmpadjolanh</a></p>
<h3 id="Smallpdf"><a href="#Smallpdf" class="headerlink" title="Smallpdf"></a>Smallpdf</h3><p>轻松易用的PDF工具，可用于编辑、转换、合并、拆分和压缩PDF文件，可与Gmail整合。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-f6f8a7bd80b9ebd5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>Chrome扩展地址<a href="https://chrome.google.com/webstore/detail/smallpdf/ohfgljdgelakfkefopgklcohadegdpjf" target="_blank" rel="noopener">https://chrome.google.com/webstore/detail/smallpdf/ohfgljdgelakfkefopgklcohadegdpjf</a></p>
<h3 id="超级马里奥兄弟"><a href="#超级马里奥兄弟" class="headerlink" title="超级马里奥兄弟"></a>超级马里奥兄弟</h3><p>在Chrome浏览器上享受经典的超级马里奥兄弟游戏！偶尔娱乐下不错。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-8b8694eb6e0b11ef.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>使用方法：x 跳跃，z 射弹，回车键开始或暂停，左右键左右移动。<br>如果不想安装扩展还有个网页版<a href="https://www.yikm.net/" target="_blank" rel="noopener">https://www.yikm.net/</a></p>
<p>Chrome扩展地址 <a href="https://chrome.google.com/webstore/detail/super-mario-bros/ihiihnpekihcakcmjogkjncdgfdbjpnh" target="_blank" rel="noopener">https://chrome.google.com/webstore/detail/super-mario-bros/ihiihnpekihcakcmjogkjncdgfdbjpnh</a></p>
<h3 id="开发工具箱"><a href="#开发工具箱" class="headerlink" title="开发工具箱"></a>开发工具箱</h3><p>经常要做编码转换，加密什么的，这个工具非常方便，不用再谷歌了。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-f8d0595db041cef6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>Chrome扩展下载地址<a href="https://chrome.google.com/webstore/detail/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7%E7%AE%B1/mflanociobpenleccopmoanpdbcjcanm" target="_blank" rel="noopener">https://chrome.google.com/webstore/detail/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7%E7%AE%B1/mflanociobpenleccopmoanpdbcjcanm</a></p>
<h3 id="查看类似网站"><a href="#查看类似网站" class="headerlink" title="查看类似网站"></a>查看类似网站</h3><p>这个扩展能让你获得正访问的网站相关的10个类似网站结果，来看看微博类似的有Facebook,baidu。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-aa128771a88a61e2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>当然也可以在网站<a href="https://www.similarsites.com/welcome" target="_blank" rel="noopener">https://www.similarsites.com/welcome</a>输入查看。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-8c8ddcff5776f64f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>Chrome扩展地址 <a href="https://chrome.google.com/webstore/detail/similar-sites-discover-re/necpbmbhhdiplmfhmjicabdeighkndkn" target="_blank" rel="noopener">https://chrome.google.com/webstore/detail/similar-sites-discover-re/necpbmbhhdiplmfhmjicabdeighkndkn</a></p>
<h3 id="豆瓣电影传送门"><a href="#豆瓣电影传送门" class="headerlink" title="豆瓣电影传送门"></a>豆瓣电影传送门</h3><p>妈妈再也不会担心我找不到资源了，直接在豆瓣电影右侧显示可以观看电影的链接。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-6aac033c823f2453.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>Chrome扩展地址 <a href="https://chrome.google.com/webstore/detail/%E8%B1%86%E7%93%A3%E7%94%B5%E5%BD%B1%E4%BC%A0%E9%80%81%E9%97%A8/pkidecliagangmpphpelecaoogfbnihi" target="_blank" rel="noopener">https://chrome.google.com/webstore/detail/%E8%B1%86%E7%93%A3%E7%94%B5%E5%BD%B1%E4%BC%A0%E9%80%81%E9%97%A8/pkidecliagangmpphpelecaoogfbnihi</a></p>
<p>安装可能遇到的问题：该扩展程序未列在 Chrome 网上应用店中，并可能是在您不知情的情况下添加的，解决：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、首先把需要安装的第三方插件，后缀.crx 改成 .rar，然后解压，得到一个文件夹</span><br><span class="line"><span class="number">2</span>、再打开chrome:<span class="comment">//extensions/谷歌扩展应用管理，点击右上角的开发者模式，就可以看到“加载正在开发的扩展程序”这一选项。</span></span><br><span class="line"><span class="number">3</span>、选择刚才步骤<span class="number">1</span>中解压好的文件夹，确定</span><br><span class="line"><span class="number">4</span>、确认新增扩展程序，点击添加，成功添加应用程序。</span><br></pre></td></tr></table></figure></p>
<p>以上扩展的安装都需要能访问谷歌，所以你还需要谷歌助手，公众号 回复 <code>谷歌</code> 获取。</p>
<h3 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h3><p>复制图片文字<br><a href="https://projectnaptha.com/" target="_blank" rel="noopener">https://projectnaptha.com/</a><br>官网<a href="https://github.com/pomber/git-history" target="_blank" rel="noopener">Git History</a>介绍只需要将 GitHub 仓库的 『github.com』网址替换为『github.githistory.xyz』，则可以动态显示文件的提交历史<br><a href="https://10beasts.net/best-vpn/china/" target="_blank" rel="noopener">https://10beasts.net/best-vpn/china/</a><br><a href="https://laod.cn/news/googles-21st-birthday.html" target="_blank" rel="noopener">https://laod.cn/news/googles-21st-birthday.html</a><br> <a href="https://www.kanfeidie.com/idea/network/287.html" target="_blank" rel="noopener">谷歌镜像网址</a><br> RSS 阅读器了 <a href="https://mp.weixin.qq.com/s?__biz=MzI5MDM4NTYwOA==&amp;mid=2247488671&amp;idx=1&amp;sn=2c5d7a459e8dbf3345481f982ed37e0e&amp;chksm=ec21ef38db56662eb0755a9ab0cb34adbf155ead565822e83f9b2a7307f37e440dc9d9d00d6a&amp;mpshare=1&amp;scene=1&amp;srcid=&amp;sharer_sharetime=1566260194390&amp;sharer_shareid=ae8eb1508a08c1b134df82bb484ea38d#rd" target="_blank" rel="noopener">https://mp.weixin.qq.com/s?__biz=MzI5MDM4NTYwOA==&amp;mid=2247488671&amp;idx=1&amp;sn=2c5d7a459e8dbf3345481f982ed37e0e&amp;chksm=ec21ef38db56662eb0755a9ab0cb34adbf155ead565822e83f9b2a7307f37e440dc9d9d00d6a&amp;mpshare=1&amp;scene=1&amp;srcid=&amp;sharer_sharetime=1566260194390&amp;sharer_shareid=ae8eb1508a08c1b134df82bb484ea38d#rd</a><br>科学上网 <a href="https://strivebo.com/2019/02/12/%E7%AF%871%EF%BC%9A%E7%A7%91%E5%AD%A6%E4%B8%8A%E7%BD%91%E6%96%B9%E5%BC%8F%E6%80%BB%E7%BB%93/" target="_blank" rel="noopener">https://strivebo.com/2019/02/12/%E7%AF%871%EF%BC%9A%E7%A7%91%E5%AD%A6%E4%B8%8A%E7%BD%91%E6%96%B9%E5%BC%8F%E6%80%BB%E7%BB%93/</a><br>墙外<a href="https://www.bvsuper.com/index.php/archives/183/" target="_blank" rel="noopener">https://www.bvsuper.com/index.php/archives/183/</a><br><a href="https://chrome.google.com/webstore/detail/setupvpn-lifetime-free-vp/oofgbpoabipfcfjapgnbbjjaenockbdp/related?utm_source=chrome-ntp-icon" target="_blank" rel="noopener">SetupVPN  </a><br>墙外指南<a href="https://tingtalk.me/archives/" target="_blank" rel="noopener">https://tingtalk.me/archives/</a><br><a href="http://tengj.top/2018/02/17/ggcj/" target="_blank" rel="noopener">http://tengj.top/2018/02/17/ggcj/</a>  实用且堪称神器的Chrome插件推荐<br>分享一些好用的 Chrome 扩展<a href="https://4ark.me/post/549a6198.html#16-Git-History" target="_blank" rel="noopener">https://4ark.me/post/549a6198.html#16-Git-History</a><br>相见恨晚的 chrome 扩展程序<a href="https://www.v2ex.com/t/516205" target="_blank" rel="noopener">https://www.v2ex.com/t/516205</a><br>Google Chrome 有什么推荐的插件<a href="https://www.v2ex.com/t/501604" target="_blank" rel="noopener">https://www.v2ex.com/t/501604</a><br>Chrome 上面有什么可以提高程序员效率的好用的插件推荐<a href="https://cn.v2ex.com/t/602480#reply45" target="_blank" rel="noopener">https://cn.v2ex.com/t/602480#reply45</a><br>你极力推荐的 Chrome 扩展有哪些<a href="https://www.v2ex.com/t/515681" target="_blank" rel="noopener">https://www.v2ex.com/t/515681</a><br>方便地查看、整理、搜索你已经 Star 过的项目和搜索 Github 上的项目<a href="https://github.com/lvxianchao/the-fucking-github" target="_blank" rel="noopener">https://github.com/lvxianchao/the-fucking-github</a><br> Github API 的图床 Chrome 插件开发全纪录 <a href="https://www.v2ex.com/t/562408" target="_blank" rel="noopener">https://www.v2ex.com/t/562408</a><br>微信群合影<br>Chrome插件英雄榜<a href="https://juejin.im/post/5c6824076fb9a049c966374c" target="_blank" rel="noopener">https://juejin.im/post/5c6824076fb9a049c966374c</a><br>无需安装 Office，即可使用 Word、Excel、PowerPoint、OneNote 和 Sway Online<a href="https://chrome.google.com/webstore/detail/office/ndjpnladcallmjemlbaebfadecfhkepb" target="_blank" rel="noopener">https://chrome.google.com/webstore/detail/office/ndjpnladcallmjemlbaebfadecfhkepb</a><br>星球链接 <a href="https://t.zsxq.com/iE2f6mu" target="_blank" rel="noopener">https://t.zsxq.com/iE2f6mu</a><br>分享一些好用的 Chrome 扩展 <a href="https://4ark.me/post/549a6198.html" target="_blank" rel="noopener">https://4ark.me/post/549a6198.html</a><br>那些离不开的 Chrome 扩展插件 <a href="https://michael728.github.io/2015/11/26/tools-chrome-extentions/" target="_blank" rel="noopener">https://michael728.github.io/2015/11/26/tools-chrome-extentions/</a><br><a href="https://pictureknow.com/#/extension" target="_blank" rel="noopener">Chrome 插件推荐</a></p>
<p>推荐阅读:</p>
<p><a href="https://mp.weixin.qq.com/s/Xz1m-mqtCcBF_4hmGCpkUQ" target="_blank" rel="noopener">如何发一条空白的朋友圈</a></p>
<p><a href="https://mp.weixin.qq.com/s/1omFkK5PPyeJEzUTagj9qg" target="_blank" rel="noopener">免费在线听周杰伦歌曲</a></p>
<p><a href="https://mp.weixin.qq.com/s/eGDO0Y8el_dsEyriCoAgog" target="_blank" rel="noopener">那些你可能不知道的微信奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/k5lBwiDzGgSU3fh2v2Rw9A" target="_blank" rel="noopener">如何在豆瓣租房小组快速找到满意的房子</a></p>
<p><a href="https://mp.weixin.qq.com/s/qNfuWjH54WHJtx4sEE5xwA" target="_blank" rel="noopener">10%+10% 不等于 0.2？</a></p>
<p><a href="https://mp.weixin.qq.com/s/adJFh_9LH0N-vvvYaiQqXg" target="_blank" rel="noopener">Chrome 浏览器扩展神器油猴</a></p>
<p><a href="https://mp.weixin.qq.com/s/HpuInXUCjSYT7HLqhoRcCA" target="_blank" rel="noopener">那些你可能不知道的 bilibili 奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/tPvrcgYb3KTlr-jyO3hPMg" target="_blank" rel="noopener">上班工作压力太大?来玩个游戏解压下</a></p>
<p><a href="https://mp.weixin.qq.com/s/7v9LHJX05Ybv39BzrMD0Uw" target="_blank" rel="noopener">那些你可能不知道的百度产品</a></p>
<h3 id="公众号：苏生不惑"><a href="#公众号：苏生不惑" class="headerlink" title="公众号：苏生不惑"></a>公众号：苏生不惑</h3><p> <img src="https://upload-images.jianshu.io/upload_images/17817191-6e0079f95d4c0338.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="扫描二维码关注"></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/09/30/那些你可能不知道的百度产品/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/09/30/那些你可能不知道的百度产品/" itemprop="url">那些你可能不知道的百度产品</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-30T20:22:36+08:00">
                2019-09-30
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.2k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  4 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>百度家的产品可谓不少，搜索就不说了，<a href="https://www.baidu.com/more/" target="_blank" rel="noopener">https://www.baidu.com/more/</a>  这里还有很多，估计很多你也没用过。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-00ec3f328470f0e6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>除了搜索，这里推荐几个百度出品的良心产品。</p>
<h3 id="百度脑图"><a href="#百度脑图" class="headerlink" title="百度脑图"></a>百度脑图</h3><p>思维导图又叫心智图，因为它直观、形象、易于理解和梳理而受欢迎，百度脑图类似网页版的 Office，可以直接在浏览器上制作思维导图。<a href="https://naotu.baidu.com/" target="_blank" rel="noopener">https://naotu.baidu.com/</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-695365c12eceffe3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>需要登录百度账号使用。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-ffd022a9a195a165.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>另外还有桌面版，在没有互联网的情况下，依然可以使用这款免费的思维导图工具，跨平台支持 Windows/Linux/Mac OS ，下载地址<a href="https://github.com/NaoTu/DesktopNaotu/releases" target="_blank" rel="noopener">https://github.com/NaoTu/DesktopNaotu/releases</a></p>
<h3 id="百度取证"><a href="#百度取证" class="headerlink" title="百度取证"></a>百度取证</h3><p>百度取证是百度联合司法鉴定中心、公证机构，共同推出的电子数据固定及保全系统，可为用户提供取证、存证、出证的一站式在线服务，如果原创作品或知识产权被侵犯了可以试试这个。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-8ca5fd7f6f84af50.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="百度网盘"><a href="#百度网盘" class="headerlink" title="百度网盘"></a>百度网盘</h3><p>随着360云盘，新浪微盘等关闭个人存储服务，现在只剩下百度网盘这个良心产品了,现在我的网盘保存了不少文件和电影，分享文件也用的百度网盘，之前写过文章 <a href="https://mp.weixin.qq.com/s/MUIPj4OgSxAeRl5Hk-2tuw" target="_blank" rel="noopener">没有提取码怎么获取百度网盘资源？</a></p>
<p>如果嫌百度网盘下载限速很慢，可以考虑使用第三方客户端 <a href="https://pan-light.peterq.cn" target="_blank" rel="noopener">https://pan-light.peterq.cn</a> ，当然更推荐你购买会员支持下百度 <a href="https://pan.baidu.com/buy/center#/svip" target="_blank" rel="noopener">https://pan.baidu.com/buy/center#/svip</a></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-14de8d17cc82b0fd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="百度图说"><a href="#百度图说" class="headerlink" title="百度图说"></a>百度图说</h3><p><a href="https://tushuo.baidu.com/" target="_blank" rel="noopener">https://tushuo.baidu.com/</a> 向粗糙图表 Say Goodbye! 百度图说是基于Echarts一个傻瓜式图表工具，无需代码基础就可以做出屌炸天的动态图表！<a href="https://tushuo.baidu.com/prod/viewProd?prodId=dta8kgu4v3g3ick8s&amp;cli=win" target="_blank" rel="noopener">https://tushuo.baidu.com/prod/viewProd?prodId=dta8kgu4v3g3ick8s&amp;cli=win</a></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-81fd606d7a3fe19a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>登录百度账号就能很方便在线制作一个图表了。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-a0dd079af4d0d298.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>百度的其他可视化产品在<a href="https://vis.baidu.com/" target="_blank" rel="noopener">https://vis.baidu.com/</a></p>
<h3 id="百度识图"><a href="#百度识图" class="headerlink" title="百度识图"></a>百度识图</h3><p>直接输入图片地址或上传图片可以搜到图片信息。<a href="http://image.baidu.com/?fr=shitu" target="_blank" rel="noopener">http://image.baidu.com/?fr=shitu</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-27f81f6ec1079f6e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-aad33d13ae3d40e0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="百度短网址"><a href="#百度短网址" class="headerlink" title="百度短网址"></a>百度短网址</h3><p>有时候网址太长很难看，比如公众号的pc文章链接 <a href="http://mp.weixin.qq.com/s?__biz=MzIyMjg2ODExMA==&amp;mid=2247484127&amp;idx=1&amp;sn=dca59ac8530476cd8fef626da81d5aff&amp;chksm=e827a53edf502c286ecea267989f9c39576d7ce9c3f605015470f709c0cd1bbb4320d9bf5b63&amp;token=743146443&amp;lang=zh_CN#rd" target="_blank" rel="noopener">http://mp.weixin.qq.com/s?__biz=MzIyMjg2ODExMA==&amp;mid=2247484127&amp;idx=1&amp;sn=dca59ac8530476cd8fef626da81d5aff&amp;chksm=e827a53edf502c286ecea267989f9c39576d7ce9c3f605015470f709c0cd1bbb4320d9bf5b63&amp;token=743146443&amp;lang=zh_CN#rd</a> ，将链接在 <a href="https://dwz.cn/" target="_blank" rel="noopener">https://dwz.cn/</a> 转换下就能生成短链接 <a href="https://dwz.cn/jFnZD9Xp" target="_blank" rel="noopener">https://dwz.cn/jFnZD9Xp</a>  ,这个和微博的t.cn类似。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-bbbce45f8dee817c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="百度文库"><a href="#百度文库" class="headerlink" title="百度文库"></a>百度文库</h3><p><a href="https://wenku.baidu.com/" target="_blank" rel="noopener">https://wenku.baidu.com/</a> 关于百度文库之前写过文章 <a href="https://mp.weixin.qq.com/s/H8VPSGt62-tblpgWkleQoQ" target="_blank" rel="noopener">那些你可能不知道的百度文库/网盘快速下载工具</a> 免费下载文库在公众号回复 <code>文库</code> 获取下载工具。</p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-a25677374058a973.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="百度字体"><a href="#百度字体" class="headerlink" title="百度字体"></a>百度字体</h3><p><a href="http://fontstore.baidu.com/static/editor/index.html" target="_blank" rel="noopener">http://fontstore.baidu.com/static/editor/index.html</a> 害怕使用的字体侵权可以自己设计个字体。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-5f31b61701beaf8b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>当然如果自己没有设计能力可以使用免费的商用字体 <a href="https://100font.com" target="_blank" rel="noopener">https://100font.com</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-fdca2c2ef983f478.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="简单搜索"><a href="#简单搜索" class="headerlink" title="简单搜索"></a>简单搜索</h3><p><a href="http://www.searchcraft.cn/" target="_blank" rel="noopener">http://www.searchcraft.cn/</a><a href="http://secr.baidu.com/" target="_blank" rel="noopener">http://secr.baidu.com/</a></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-89550374f4f20763.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>最后说个百度的搜索特效，可能很多人没见过，比如搜索黑洞<br><img src="https://upload-images.jianshu.io/upload_images/17817191-e686e722f478461b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>搜索表白，之前也写过<a href="https://mp.weixin.qq.com/s/y8BrtwH4vlXlHKWQ9OMhdw" target="_blank" rel="noopener">听说你想 520 表白</a></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-715f2467623de738.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>还有很多这样的关键词彩蛋，比如 抖动， duang，旋转等，可以自己搜索体验下，第一次看到的时候还有点惊艳。更多搜索技巧看之前的文章 <a href="https://mp.weixin.qq.com/s/-5tZWfeWWa_E8jRCH0T_Cw" target="_blank" rel="noopener">那些你可能不知道的搜索奇技淫巧</a></p>
<h3 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h3><p>氢小说(H2 Book)是我们定义的一种对话体、类剧本式的开放图书格式。它采用对话和场景来展现故事、描述事实，又非常接近于我们平时使用的聊天软件，所以读起来更为轻松。<a href="https://github.com/easychen/h2reader-host" target="_blank" rel="noopener">https://github.com/easychen/h2reader-host</a> <a href="https://qing.ftqq.com/#/" target="_blank" rel="noopener">https://qing.ftqq.com/#/</a></p>
<p>【网易见外】为视频自动生成字幕、音频转文字等等 <a href="https://jianwai.netease.com/" target="_blank" rel="noopener">https://jianwai.netease.com/</a><br><a href="https://cn.v2ex.com/t/592533#reply12" target="_blank" rel="noopener">百度识图可以用来当免费的图床，还不防盗链</a><br>不限速 的百度网盘客户端 <a href="https://github.com/peterq/pan-light" target="_blank" rel="noopener">https://github.com/peterq/pan-light</a><br>输入网盘链接和提取码<a href="https://labs.nazo.moe/netdisk.html" target="_blank" rel="noopener">https://labs.nazo.moe/netdisk.html</a> NetDisk Downloader<br>百度<a href="http://cnm.buhuibaidu.me" target="_blank" rel="noopener">http://cnm.buhuibaidu.me</a><br><a href="https://www.echartsjs.com/zh/index.html" target="_blank" rel="noopener">https://www.echartsjs.com/zh/index.html</a><br><a href="https://www.zhihu.com/question/20512556" target="_blank" rel="noopener">https://www.zhihu.com/question/20512556</a><br> <a href="https://www.zhihu.com/question/39567389" target="_blank" rel="noopener">https://www.zhihu.com/question/39567389</a><br><a href="https://www.zhihu.com/question/39371134" target="_blank" rel="noopener">https://www.zhihu.com/question/39371134</a><br>BAT出品的PPT神器<a href="https://zhuanlan.zhihu.com/p/28686098" target="_blank" rel="noopener">https://zhuanlan.zhihu.com/p/28686098</a><br><a href="https://www.baiduyun.wiki/" target="_blank" rel="noopener">百度网盘直链下载助手</a><br><a href="https://www.bilibili.com/read/cv2705594/" target="_blank" rel="noopener">手把手教你满速下载百度网盘文件</a><br> 2019 百度云不限速下载教程 <a href="https://cn.v2ex.com/t/600571#reply32" target="_blank" rel="noopener">https://cn.v2ex.com/t/600571#reply32</a><br>当然要用 chrome 的 aria2 插件啦 <a href="https://github.com/acgotaku/BaiduExporter" target="_blank" rel="noopener">https://github.com/acgotaku/BaiduExporter</a><br>免费在线思维导图软件 <a href="https://gitmind.cn/" target="_blank" rel="noopener">https://gitmind.cn/</a> 腾讯8分钟<a href="https://app.gitmind.cn/doc/5054448" target="_blank" rel="noopener">https://app.gitmind.cn/doc/5054448</a><br>在线协作编辑器 <a href="http://we.woshifyz.com/" target="_blank" rel="noopener">http://we.woshifyz.com/</a></p>
<p><code>如果文章对你有帮助还望不吝点个在看支持下，感谢。</code></p>
<p>推荐阅读:</p>
<p><a href="https://mp.weixin.qq.com/s/Xz1m-mqtCcBF_4hmGCpkUQ" target="_blank" rel="noopener">如何发一条空白的朋友圈</a></p>
<p><a href="https://mp.weixin.qq.com/s/1omFkK5PPyeJEzUTagj9qg" target="_blank" rel="noopener">免费在线听周杰伦歌曲</a></p>
<p><a href="https://mp.weixin.qq.com/s/eGDO0Y8el_dsEyriCoAgog" target="_blank" rel="noopener">那些你可能不知道的微信奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/k5lBwiDzGgSU3fh2v2Rw9A" target="_blank" rel="noopener">如何在豆瓣租房小组快速找到满意的房子</a></p>
<p><a href="https://mp.weixin.qq.com/s/qNfuWjH54WHJtx4sEE5xwA" target="_blank" rel="noopener">10%+10% 不等于 0.2？</a></p>
<p><a href="https://mp.weixin.qq.com/s/adJFh_9LH0N-vvvYaiQqXg" target="_blank" rel="noopener">Chrome 浏览器扩展神器油猴</a></p>
<h3 id="公众号：苏生不惑"><a href="#公众号：苏生不惑" class="headerlink" title="公众号：苏生不惑"></a>公众号：苏生不惑</h3><p> <img src="https://upload-images.jianshu.io/upload_images/17817191-6e0079f95d4c0338.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="扫描二维码关注"></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/09/30/上班工作压力太大-来玩个游戏解压下/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/09/30/上班工作压力太大-来玩个游戏解压下/" itemprop="url">上班工作压力太大?来玩个游戏解压下</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-30T20:21:41+08:00">
                2019-09-30
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  942 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  3 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p> 996工作压力太大，下班或者周末来玩几个解压的小游戏缓解下。</p>
<h3 id="毒鸡汤"><a href="#毒鸡汤" class="headerlink" title="毒鸡汤"></a>毒鸡汤</h3><p>先来碗毒鸡汤 <a href="http://nows.fun/" target="_blank" rel="noopener">http://nows.fun/</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-2a26f5e53a052852.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-c4cfe23ec919c270.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="太鼓达人"><a href="#太鼓达人" class="headerlink" title="太鼓达人"></a>太鼓达人</h3><p>这个解压小游戏源自日本网站 <a href="https://aidn.jp/mikuwarp/" target="_blank" rel="noopener">https://aidn.jp/mikuwarp/</a> 一个基于初音未来创作的减压神器，点击屏幕即可生成一段音乐，他还有其他游戏在<a href="https://aidn.jp/contents/" target="_blank" rel="noopener">https://aidn.jp/contents/</a></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-ddd8ecc9d52ddd2e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="还有"></p>
<p>因为访问速度的原因有人开发了国内版本 <a href="https://static.hfi.me/mikutap/，而且还开源了" target="_blank" rel="noopener">https://static.hfi.me/mikutap/，而且还开源了</a><br> <a href="https://github.com/HFIProgramming/mikutap" target="_blank" rel="noopener">https://github.com/HFIProgramming/mikutap</a> ，它是通过点击屏幕来让Miku发出可爱的声音，这个游戏非常简单，靠指尖触碰就可以自己创作一首歌曲。 </p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-8c36390c0d52a182.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>电脑上可以疯狂敲键盘或者鼠标，手机上可以疯狂点击屏幕。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-441437543e17460a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>生成的音乐可以用 ScreenToGif   等软件录下来。</p>
<p>微博上不少人玩过<br><img src="https://upload-images.jianshu.io/upload_images/17817191-5e880964ffcf9cca.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>对应的还有安卓版 <a href="https://www.coolapk.com/apk/at.rbq.mikutap" target="_blank" rel="noopener">https://www.coolapk.com/apk/at.rbq.mikutap</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-2fc3dfee726f1bc2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="小霸王"><a href="#小霸王" class="headerlink" title="小霸王"></a>小霸王</h3><p>超级玛丽和魂斗罗等游戏很多人小时候玩过，这是网页版的小霸王 <a href="https://www.yikm.net/" target="_blank" rel="noopener">https://www.yikm.net/</a>  可以让你重温童年的乐趣。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-6b4a9c389559fcc4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-98b3766b8116429f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-398241be3e05e139.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>PC 端 w s a d 分别是上下左右，j k 分别是攻击和跳，b 是选择，回车是开始。你要不来试试？<br><img src="https://upload-images.jianshu.io/upload_images/17817191-e7a822cc8cd605dc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="手鼓猫"><a href="#手鼓猫" class="headerlink" title="手鼓猫"></a>手鼓猫</h3><p>一只可爱的小猫在桌子上敲手鼓，玩法：A&amp;D 键位可以敲鼓，C 键敲锣，Q&amp;W…&amp;O&amp;P键位可以弹竖琴，数字键弹钢琴，空格键用来发出喵叫，适合放松下手指，在线试玩：<a href="https://bongo.cat/" target="_blank" rel="noopener">https://bongo.cat/</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-943730a48f85899b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>比如敲以下数字是一首生日快乐歌。<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> <span class="number">1</span> <span class="number">3</span> <span class="number">1</span> <span class="number">6</span> <span class="number">5</span></span><br><span class="line"><span class="number">1</span> <span class="number">1</span> <span class="number">3</span> <span class="number">1</span> <span class="number">8</span> <span class="number">6</span></span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-f499c149d5519e7c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="滑雪撬"><a href="#滑雪撬" class="headerlink" title="滑雪撬"></a>滑雪撬</h3><p>一个小游戏，用笔画出一条线，小人就沿着线滑雪撬，可以翻跟头，下滑，倒退，如果你画出一个满意的线路，还可以保存成视频，在线试玩：<a href="https://www.linerider.com/" target="_blank" rel="noopener">https://www.linerider.com/</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-c6b405d8939f3685.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h3><p>玩了一个上午Mikutap之后，我理解了它走红的原因<a href="https://www.qdaily.com/articles/56872.html" target="_blank" rel="noopener">https://www.qdaily.com/articles/56872.html</a><br>   <a href="https://segmentfault.com/a/1190000016791939" target="_blank" rel="noopener">基于canvas和web audio实现低配版MikuTap</a><br><a href="https://github.com/zzuzsj/ZsaiMiku.git" target="_blank" rel="noopener">https://github.com/zzuzsj/ZsaiMiku.git</a><br>分享一个超带感有趣的网站mikutap <a href="https://www.qtbox.cn/share/miku.html" target="_blank" rel="noopener">https://www.qtbox.cn/share/miku.html</a>  <a href="https://www.qtbox.cn/miku/" target="_blank" rel="noopener">https://www.qtbox.cn/miku/</a><br><a href="https://www.v2ex.com/t/558899?p=1" target="_blank" rel="noopener">https://www.v2ex.com/t/558899?p=1</a><br>一款有趣的音乐解压游戏 <a href="https://www.cocosec.com/archives/74.html" target="_blank" rel="noopener">https://www.cocosec.com/archives/74.html</a><br> 小霸王：<a href="https://www.yikm.net" target="_blank" rel="noopener">https://www.yikm.net</a> <a href="https://www.v2ex.com/t/403054" target="_blank" rel="noopener">https://www.v2ex.com/t/403054</a><br>在线 dos 游戏：<a href="https://dos.zczc.cz/" target="_blank" rel="noopener">https://dos.zczc.cz/</a><br>Bongo Cat 手鼓猫：A&amp;D 键位可以敲鼓，C 键敲锣，Q&amp;W…&amp;O&amp;P键位可以弹竖琴，数字键弹钢琴，空格键用来发出喵叫，适合放松下手指摸个鱼，在线试玩：<a href="https://bongo.cat/" target="_blank" rel="noopener">https://bongo.cat/</a>  <a href="https://github.com/Externalizable/bongo.cat" target="_blank" rel="noopener">https://github.com/Externalizable/bongo.cat</a><br>magickeyboard：一个摸鱼解压网站，会将你输入的字符从屏幕下方弹起，并伴有清脆的敲击声。在线访问：<a href="http://magickeyboard.io/" target="_blank" rel="noopener">http://magickeyboard.io/</a>  <a href="https://github.com/feross/magickeyboard.io" target="_blank" rel="noopener">https://github.com/feross/magickeyboard.io</a><br>撘楼分享个摸鱼网站： <a href="https://tophub.fun" target="_blank" rel="noopener">https://tophub.fun</a>  全平台支持 <a href="https://www.printf520.com/hot.html" target="_blank" rel="noopener">https://www.printf520.com/hot.html</a><br>主要功能是对整个文档进行翻译，100多种不同语言文档可任意转换，无需注册<a href="https://www.onlinedoctranslator.com/" target="_blank" rel="noopener">https://www.onlinedoctranslator.com/</a><br>塔防：<a href="https://yorg.io/" target="_blank" rel="noopener">https://yorg.io/</a><br>linerider：一个小游戏，用笔画出一条线，小人就沿着线滑雪撬，可以翻跟头可以下滑可以倒退，N种不同的结果全在你的掌握，如果你画出一个满意的线路，还可以保存成视频~在线试玩：<a href="https://www.linerider.com/" target="_blank" rel="noopener">https://www.linerider.com/</a><br>游戏合集：<a href="http://iogames.space/" target="_blank" rel="noopener">http://iogames.space/</a> </p>
<p>推荐阅读:</p>
<p><a href="https://mp.weixin.qq.com/s/Xz1m-mqtCcBF_4hmGCpkUQ" target="_blank" rel="noopener">如何发一条空白的朋友圈</a></p>
<p><a href="https://mp.weixin.qq.com/s/1omFkK5PPyeJEzUTagj9qg" target="_blank" rel="noopener">免费在线听周杰伦歌曲</a></p>
<p><a href="https://mp.weixin.qq.com/s/eGDO0Y8el_dsEyriCoAgog" target="_blank" rel="noopener">那些你可能不知道的微信奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/k5lBwiDzGgSU3fh2v2Rw9A" target="_blank" rel="noopener">如何在豆瓣租房小组快速找到满意的房子</a></p>
<p><a href="https://mp.weixin.qq.com/s/qNfuWjH54WHJtx4sEE5xwA" target="_blank" rel="noopener">10%+10% 不等于 0.2？</a></p>
<p><a href="https://mp.weixin.qq.com/s/adJFh_9LH0N-vvvYaiQqXg" target="_blank" rel="noopener">Chrome 浏览器扩展神器油猴</a></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-393b26173c148690.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="免费星球"></p>
<h3 id="公众号：苏生不惑"><a href="#公众号：苏生不惑" class="headerlink" title="公众号：苏生不惑"></a>公众号：苏生不惑</h3><p> <img src="https://upload-images.jianshu.io/upload_images/17817191-6e0079f95d4c0338.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="扫描二维码关注"></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/09/16/Python-抓取知识星球内容生成-PDF/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/09/16/Python-抓取知识星球内容生成-PDF/" itemprop="url">Python 抓取知识星球内容生成 PDF</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-16T20:29:10+08:00">
                2019-09-16
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  4.4k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  21 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>知识星球是什么?</p>
<blockquote>
<p>知识星球是创作者连接铁杆粉丝，做出高品质社群，实现知识变现的工具。创作者可以用知识星球连接铁杆粉丝，做出高品质社群，实现知识变现。<br>知识星球解决的核心问题是社群收费管理难问题和内容不能沉淀问题。微信公众号、微博和行业专家——这些有粉丝的创作者是知识星球的核心用户，都可以用知识星球运营社群，知识变现。</p>
</blockquote>
<p>以上来自知识星球官网的介绍 <a href="https://help.zsxq.com/" target="_blank" rel="noopener">https://help.zsxq.com/</a> 口号是连接1000位铁杆粉丝。</p>
<h3 id="为什么用星球"><a href="#为什么用星球" class="headerlink" title="为什么用星球"></a>为什么用星球</h3><p>我没做过社群，也不是什么行业专家，毕竟不是什么大v，为什么要用知识星球呢？主要是现在获取的资讯太多了，想沉淀记录些东西，方便自己，也方便他人找，为什么不用微博呢？微博用了很多年，每天都在更新，目前已经8万多条微博了。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-840f9df6851655c4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>微博上有很多mark党，不断转发微博，但几乎没再去看过，不知道你是否也这样，以为收藏就看过了，其实只是种心里安慰。</p>
<p>而且微博上转发的东西经常被删，微博太多管理起来也麻烦，于是6月份的时候建立了一个免费的星球，就是这个了<a href="https://wx.zsxq.com/dweb2/index/group/141281112142" target="_blank" rel="noopener">https://wx.zsxq.com/dweb2/index/group/141281112142</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-935a32b13ae6980f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-393b26173c148690.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="免费星球"><br>星球还可以上传文件，和微博一样加标签方便分类，还提供网页版，比微信方便多了。</p>
<h3 id="导出星球"><a href="#导出星球" class="headerlink" title="导出星球"></a>导出星球</h3><p>过去3个月更新几百条信息了，也都加好标签。现在有200多个小伙伴了，你有兴趣也加入吧。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-c78802c9c772446f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-0bc2f593ae8857eb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>比如工具这个标签列表的内容<a href="https://wx.zsxq.com/dweb2/index/tags/%E5%B7%A5%E5%85%B7/828821151512" target="_blank" rel="noopener">https://wx.zsxq.com/dweb2/index/tags/%E5%B7%A5%E5%85%B7/828821151512</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-2a3fe75088709eb8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>但内容多了以后翻起来也麻烦，于是想着下载下来看，最好能导出PDF，于是准备研究下，搜索下发现有人已经做过了 <a href="https://github.com/wbsabc/zsxq-spider" target="_blank" rel="noopener">https://github.com/wbsabc/zsxq-spider</a> <a href="https://github.com/96chh/crawl-zsxq" target="_blank" rel="noopener">https://github.com/96chh/crawl-zsxq</a><br>思路为抓取网页版的接口<a href="https://api.zsxq.com/v1.10/groups/141281112142/topics?scope=all&amp;count=20" target="_blank" rel="noopener">https://api.zsxq.com/v1.10/groups/141281112142/topics?scope=all&amp;count=20</a> 每次加载20条，每次的最后一条的create_time为下次的开始时间，如果没有20条说明加载完了。不过他的代码还有些问题，需要改动下，于是开始动手了。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> re</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> json</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> pdfkit</span><br><span class="line"><span class="keyword">import</span> shutil</span><br><span class="line"><span class="keyword">import</span> datetime</span><br><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"><span class="keyword">from</span> urllib.parse <span class="keyword">import</span> quote</span><br><span class="line"><span class="keyword">from</span> urllib.parse <span class="keyword">import</span> unquote</span><br><span class="line"><span class="keyword">import</span> re,jieba,pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"><span class="keyword">from</span> wordcloud <span class="keyword">import</span> WordCloud, STOPWORDS, ImageColorGenerator</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">from</span> pyquery <span class="keyword">import</span> PyQuery</span><br><span class="line">ZSXQ_ACCESS_TOKEN = '2B9BA4A2-8CAB-3A7C-32E7-' # 登录后Cookie中的Token</span><br><span class="line">GROUP_ID = '142452821282'                                  # 知识星球中的小组ID 141281112142  每日分享554228114224</span><br><span class="line">PDF_FILE_NAME = '知识星球_斜杠星球.pdf'                               # 生成PDF文件的名字</span><br><span class="line">DOWLOAD_PICS = False                                        # 是否下载图片 True | False 下载会导致程序变慢</span><br><span class="line">DOWLOAD_COMMENTS = False                                    # 是否下载评论</span><br><span class="line">ONLY_DIGESTS = False                                       # True-只精华 | False-全部</span><br><span class="line">FROM_DATE_TO_DATE = False                                  # 按时间区间下载</span><br><span class="line">EARLY_DATE = '2017-05-25T00:00:00.000+0800'                # 最早时间 当FROM_DATE_TO_DATE=True时生效 为空表示不限制 形如'2017-05-25T00:00:00.000+0800'</span><br><span class="line">LATE_DATE = '2019-09-25T00:00:00.000+0800'                 # 最晚时间 当FROM_DATE_TO_DATE=True时生效 为空表示不限制 形如'2017-05-25T00:00:00.000+0800'</span><br><span class="line">DELETE_PICS_WHEN_DONE = True                               # 运行完毕后是否删除下载的图片</span><br><span class="line">DELETE_HTML_WHEN_DONE = True                               # 运行完毕后是否删除生成的HTML</span><br><span class="line">COUNTS_PER_TIME = 30                                       # 每次请求加载几个主题 最大可设置为30</span><br><span class="line">DEBUG = False                                              # DEBUG开关</span><br><span class="line">DEBUG_NUM = 120                                            # DEBUG时 跑多少条数据后停止 需与COUNTS_PER_TIME结合考虑</span><br><span class="line"></span><br><span class="line">html_template = <span class="string">""</span><span class="string">"</span></span><br><span class="line"><span class="string">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="string">&lt;html lang="</span>en<span class="string">"&gt;</span></span><br><span class="line"><span class="string">&lt;head&gt;</span></span><br><span class="line"><span class="string">    &lt;meta charset="</span>UTF<span class="number">-8</span><span class="string">"&gt;</span></span><br><span class="line"><span class="string">&lt;/head&gt;</span></span><br><span class="line"><span class="string">&lt;body&gt;</span></span><br><span class="line"><span class="string">&lt;h1&gt;&#123;title&#125;&lt;/h1&gt;</span></span><br><span class="line"><span class="string">&lt;br&gt;&#123;author&#125; - &#123;cretime&#125;&lt;br&gt;</span></span><br><span class="line"><span class="string">&lt;p&gt;&#123;text&#125;&lt;/p&gt;</span></span><br><span class="line"><span class="string">&lt;/body&gt;</span></span><br><span class="line"><span class="string">&lt;/html&gt;</span></span><br><span class="line"><span class="string">"</span><span class="string">""</span></span><br><span class="line">htmls = []</span><br><span class="line">num = <span class="number">0</span></span><br><span class="line">def headers_to_dict(headers):</span><br><span class="line">    <span class="string">""</span><span class="string">"</span></span><br><span class="line"><span class="string">    将字符串</span></span><br><span class="line"><span class="string">    '''</span></span><br><span class="line"><span class="string">    Host: mp.weixin.qq.com</span></span><br><span class="line"><span class="string">    Connection: keep-alive</span></span><br><span class="line"><span class="string">    Cache-Control: max-age=</span></span><br><span class="line"><span class="string">    '''</span></span><br><span class="line"><span class="string">    转换成字典类型</span></span><br><span class="line"><span class="string">    :param headers: str</span></span><br><span class="line"><span class="string">    :return: dict</span></span><br><span class="line"><span class="string">    "</span><span class="string">""</span></span><br><span class="line">    headers = headers.split(<span class="string">"\n"</span>)</span><br><span class="line">    d_headers = dict()</span><br><span class="line">    <span class="keyword">for</span> h <span class="keyword">in</span> headers:</span><br><span class="line">        h = h.strip()</span><br><span class="line">        <span class="keyword">if</span> h:</span><br><span class="line">            k, v = h.split(<span class="string">":"</span>, <span class="number">1</span>)</span><br><span class="line">            d_headers[k] = v.strip()</span><br><span class="line">    <span class="keyword">return</span> d_headers</span><br><span class="line">def get_data(url):</span><br><span class="line"></span><br><span class="line">    OVER_DATE_BREAK = False</span><br><span class="line"></span><br><span class="line">    global htmls, num</span><br><span class="line">        </span><br><span class="line">   </span><br><span class="line">    header = <span class="string">""</span><span class="string">"</span></span><br><span class="line"><span class="string">Accept:application/json, text/plain, */*</span></span><br><span class="line"><span class="string">Accept-Encoding:gzip, deflate, br</span></span><br><span class="line"><span class="string">Accept-Language:zh-CN</span></span><br><span class="line"><span class="string">Connection:keep-alive</span></span><br><span class="line"><span class="string">Cookie:sensorsdata2015jssdkcross=%7B%22dis ; zsxq_access_token=BA53 6025A</span></span><br><span class="line"><span class="string">DNT:1</span></span><br><span class="line"><span class="string">Host:api.zsxq.com</span></span><br><span class="line"><span class="string">Origin:https://wx.zsxq.com</span></span><br><span class="line"><span class="string">Referer:https://wx.zsxq.com/dweb2/index/group/224445125221</span></span><br><span class="line"><span class="string">User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36 Maxthon/5.2.7.5000</span></span><br><span class="line"><span class="string">X-Request-Id:fcf935489-5997-674e-9db2-a02ea9b389f</span></span><br><span class="line"><span class="string">X-Signature:81b69d88df26231776893554ec8d733448743908</span></span><br><span class="line"><span class="string">X-Timestamp:1567682270</span></span><br><span class="line"><span class="string">X-Version:1.10.17</span></span><br><span class="line"><span class="string">    "</span><span class="string">""</span></span><br><span class="line">    headers = headers_to_dict(header)</span><br><span class="line">    t = []</span><br><span class="line">    </span><br><span class="line">    rsp = requests.get(url, headers=headers)</span><br><span class="line">    print(url,headers,rsp.json())</span><br><span class="line">    <span class="string">""</span><span class="string">"</span></span><br><span class="line"><span class="string">    cat temp.json</span></span><br><span class="line"><span class="string">    &#123;</span></span><br><span class="line"><span class="string">      "</span>succeeded<span class="string">": true,</span></span><br><span class="line"><span class="string">      "</span>resp_data<span class="string">": &#123;</span></span><br><span class="line"><span class="string">        "</span>topics<span class="string">": []</span></span><br><span class="line"><span class="string">      &#125;</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">    cat temp.css</span></span><br><span class="line"><span class="string">    h1 &#123;font-size:40px; color:red; text-align:center;&#125;</span></span><br><span class="line"><span class="string">    p &#123;font-size:30px;&#125;</span></span><br><span class="line"><span class="string">    img&#123;</span></span><br><span class="line"><span class="string">    	max-width:100%;</span></span><br><span class="line"><span class="string">    	margin:20px auto;</span></span><br><span class="line"><span class="string">    	height:auto;</span></span><br><span class="line"><span class="string">    	border:0;</span></span><br><span class="line"><span class="string">    	outline:0</span></span><br><span class="line"><span class="string">    	-webkit-box-shadow: 1px 4px 16px 8px #5CA2BE;</span></span><br><span class="line"><span class="string">        -moz-box-shadow: 1px 4px 16px 8px #5CA2BE;</span></span><br><span class="line"><span class="string">        box-shadow: 1px 4px 16px 8px #5CA2BE;</span></span><br><span class="line"><span class="string">        /*set the images aligned*/</span></span><br><span class="line"><span class="string">        display: block;</span></span><br><span class="line"><span class="string">        margin-left: auto;</span></span><br><span class="line"><span class="string">        margin-right: auto;</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">    "</span><span class="string">""</span></span><br><span class="line">    with open('temp.json', 'w', encoding='utf-8') as f: # 将返回数据写入temp.json方便查看</span><br><span class="line">        f.write(json.dumps(rsp.json(), indent=<span class="number">2</span>, ensure_ascii=False))</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">with</span> open(<span class="string">'temp.json'</span>, encoding=<span class="string">'utf-8'</span>) <span class="keyword">as</span> f, open(<span class="string">'contents2.txt'</span>, <span class="string">'a+'</span>, encoding=<span class="string">'utf-8'</span>) <span class="keyword">as</span> f2:</span><br><span class="line">        <span class="keyword">for</span> topic <span class="keyword">in</span> json.loads(f.read()).get(<span class="string">'resp_data'</span>).get(<span class="string">'topics'</span>):</span><br><span class="line">            <span class="keyword">if</span> FROM_DATE_TO_DATE and EARLY_DATE.strip():</span><br><span class="line">                <span class="keyword">if</span> topic.get(<span class="string">'create_time'</span>) &lt; EARLY_DATE.strip():</span><br><span class="line">                    OVER_DATE_BREAK = True</span><br><span class="line">                    <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line">            content = topic.get(<span class="string">'question'</span>, topic.get(<span class="string">'talk'</span>, topic.get(<span class="string">'task'</span>, topic.get(<span class="string">'solution'</span>))))</span><br><span class="line"></span><br><span class="line">            anonymous = content.get(<span class="string">'anonymous'</span>)</span><br><span class="line">            <span class="keyword">if</span> anonymous:</span><br><span class="line">                author = <span class="string">'匿名用户'</span></span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                author = content.get(<span class="string">'owner'</span>).get(<span class="string">'name'</span>)</span><br><span class="line"></span><br><span class="line">            cretime = (topic.get(<span class="string">'create_time'</span>)[:<span class="number">23</span>]).replace(<span class="string">'T'</span>, <span class="string">' '</span>)</span><br><span class="line"></span><br><span class="line">            text = content.get(<span class="string">'text'</span>, <span class="string">''</span>)</span><br><span class="line">            f2.write(text)</span><br><span class="line"></span><br><span class="line">            text = handle_link(text)</span><br><span class="line">            #f2.write(PyQuery(text).text())</span><br><span class="line">            t.append(text)</span><br><span class="line">            title = str(num) + <span class="string">'_'</span> + cretime[:<span class="number">16</span>]</span><br><span class="line">            num += <span class="number">1</span></span><br><span class="line">            <span class="keyword">if</span> topic.get(<span class="string">'digested'</span>) == True:</span><br><span class="line">                title += <span class="string">'_精华'</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> DOWLOAD_PICS and content.get(<span class="string">'images'</span>):</span><br><span class="line">                soup = BeautifulSoup(html_template, <span class="string">'html.parser'</span>)</span><br><span class="line">                images_index = <span class="number">0</span></span><br><span class="line">                <span class="keyword">for</span> img <span class="keyword">in</span> content.get(<span class="string">'images'</span>):</span><br><span class="line">                    url = img.get(<span class="string">'large'</span>).get(<span class="string">'url'</span>)</span><br><span class="line">                    local_url = <span class="string">'./images/'</span> + str(num - <span class="number">1</span>) + <span class="string">'_'</span> + str(images_index) + <span class="string">'.jpg'</span></span><br><span class="line">                    images_index += <span class="number">1</span></span><br><span class="line">                    urllib.request.urlretrieve(url, local_url)</span><br><span class="line">                    img_tag = soup.new_tag(<span class="string">'img'</span>, src=local_url)</span><br><span class="line">                    soup.body.append(img_tag)</span><br><span class="line">                html_img = str(soup)</span><br><span class="line">                html = html_img.format(title=title, text=text, author=author, cretime=cretime)</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                html = html_template.format(title=title, text=text, author=author, cretime=cretime)</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> topic.get(<span class="string">'question'</span>):</span><br><span class="line">                answer_author = topic.get(<span class="string">'answer'</span>).get(<span class="string">'owner'</span>).get(<span class="string">'name'</span>, <span class="string">''</span>)</span><br><span class="line">                answer = topic.get(<span class="string">'answer'</span>).get(<span class="string">'text'</span>, <span class="string">""</span>)</span><br><span class="line">                answer = handle_link(answer)</span><br><span class="line"></span><br><span class="line">                soup = BeautifulSoup(html, <span class="string">'html.parser'</span>)</span><br><span class="line">                answer_tag = soup.new_tag(<span class="string">'p'</span>)</span><br><span class="line"></span><br><span class="line">                answer = <span class="string">'【'</span> + answer_author + <span class="string">'】 回答：&lt;br&gt;'</span> + answer</span><br><span class="line">                soup_temp = BeautifulSoup(answer, <span class="string">'html.parser'</span>)</span><br><span class="line">                answer_tag.append(soup_temp)</span><br><span class="line"></span><br><span class="line">                soup.body.append(answer_tag)</span><br><span class="line">                html = str(soup) </span><br><span class="line">			</span><br><span class="line">            files = content.get(<span class="string">'files'</span>)</span><br><span class="line">            <span class="keyword">if</span> files:</span><br><span class="line">                files_content = <span class="string">'&lt;i&gt;文件列表(需访问网站下载) :&lt;br&gt;'</span></span><br><span class="line">                <span class="keyword">for</span> f <span class="keyword">in</span> files:</span><br><span class="line">                    files_content += f.get(<span class="string">'name'</span>) + <span class="string">'&lt;br&gt;'</span></span><br><span class="line">                files_content += <span class="string">'&lt;/i&gt;'</span></span><br><span class="line">                soup = BeautifulSoup(html, <span class="string">'html.parser'</span>)</span><br><span class="line">                files_tag = soup.new_tag(<span class="string">'p'</span>)</span><br><span class="line">                soup_temp = BeautifulSoup(files_content, <span class="string">'html.parser'</span>)</span><br><span class="line">                files_tag.append(soup_temp)</span><br><span class="line">                soup.body.append(files_tag)</span><br><span class="line">                html = str(soup)</span><br><span class="line"></span><br><span class="line">            comments = topic.get(<span class="string">'show_comments'</span>)</span><br><span class="line">            <span class="keyword">if</span> DOWLOAD_COMMENTS and comments:</span><br><span class="line">                soup = BeautifulSoup(html, <span class="string">'html.parser'</span>)</span><br><span class="line">                hr_tag = soup.new_tag(<span class="string">'hr'</span>)</span><br><span class="line">                soup.body.append(hr_tag)</span><br><span class="line">                <span class="keyword">for</span> comment <span class="keyword">in</span> comments:</span><br><span class="line">                    comment_str = <span class="string">''</span></span><br><span class="line">                    <span class="keyword">if</span> comment.get(<span class="string">'repliee'</span>):</span><br><span class="line">                        comment_str = <span class="string">'['</span> + comment.get(<span class="string">'owner'</span>).get(<span class="string">'name'</span>) + <span class="string">' 回复 '</span> + comment.get(<span class="string">'repliee'</span>).get(<span class="string">'name'</span>) + <span class="string">'] : '</span> + handle_link(comment.get(<span class="string">'text'</span>))</span><br><span class="line">                    <span class="keyword">else</span>:</span><br><span class="line">                        comment_str = <span class="string">'['</span> + comment.get(<span class="string">'owner'</span>).get(<span class="string">'name'</span>) + <span class="string">'] : '</span> + handle_link(comment.get(<span class="string">'text'</span>))</span><br><span class="line"></span><br><span class="line">                    comment_tag = soup.new_tag(<span class="string">'p'</span>)</span><br><span class="line">                    soup_temp = BeautifulSoup(comment_str, <span class="string">'html.parser'</span>)</span><br><span class="line">                    comment_tag.append(soup_temp)</span><br><span class="line">                    soup.body.append(comment_tag)</span><br><span class="line">                html = str(soup)</span><br><span class="line"></span><br><span class="line">            htmls.append(html)</span><br><span class="line"></span><br><span class="line">    # DEBUG 仅导出部分数据时使用</span><br><span class="line">    <span class="keyword">if</span> DEBUG and num &gt;= DEBUG_NUM:</span><br><span class="line">       <span class="keyword">return</span> htmls</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> OVER_DATE_BREAK:</span><br><span class="line">        <span class="keyword">return</span> htmls</span><br><span class="line"></span><br><span class="line">    next_page = rsp.json().get(<span class="string">'resp_data'</span>).get(<span class="string">'topics'</span>)</span><br><span class="line">    <span class="keyword">if</span> next_page:</span><br><span class="line">        create_time = next_page[<span class="number">-1</span>].get(<span class="string">'create_time'</span>)</span><br><span class="line">        <span class="keyword">if</span> create_time[<span class="number">20</span>:<span class="number">23</span>] == <span class="string">"000"</span>:</span><br><span class="line">            end_time = create_time[:<span class="number">20</span>]+<span class="string">"999"</span>+create_time[<span class="number">23</span>:]</span><br><span class="line">            str_date_time = end_time[:<span class="number">19</span>]</span><br><span class="line">            delta = datetime.timedelta(seconds=<span class="number">1</span>)</span><br><span class="line">            date_time = datetime.datetime.strptime(str_date_time, <span class="string">'%Y-%m-%dT%H:%M:%S'</span>)</span><br><span class="line">            date_time = date_time - delta</span><br><span class="line">            str_date_time = date_time.strftime(<span class="string">'%Y-%m-%dT%H:%M:%S'</span>)</span><br><span class="line">            end_time = str_date_time + end_time[<span class="number">19</span>:]</span><br><span class="line">        <span class="keyword">else</span> :</span><br><span class="line">            res = int(create_time[<span class="number">20</span>:<span class="number">23</span>])<span class="number">-1</span></span><br><span class="line">            end_time = create_time[:20]+str(res).zfill(3)+create_time[23:] # zfill 函数补足结果前面的零，始终为3位数</span><br><span class="line">        end_time = quote(end_time)</span><br><span class="line">        <span class="keyword">if</span> len(end_time) == <span class="number">33</span>:</span><br><span class="line">            end_time = end_time[:<span class="number">24</span>] + <span class="string">'0'</span> + end_time[<span class="number">24</span>:]</span><br><span class="line">        next_url = start_url + <span class="string">'&amp;end_time='</span> + end_time</span><br><span class="line">        print(next_url)</span><br><span class="line">        get_data(next_url)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> htmls,t</span><br><span class="line"></span><br><span class="line">def handle_link(text):</span><br><span class="line">    soup = BeautifulSoup(text, <span class="string">"html.parser"</span>)</span><br><span class="line"></span><br><span class="line">    mention = soup.find_all(<span class="string">'e'</span>, attrs=&#123;<span class="string">'type'</span> : <span class="string">'mention'</span>&#125;)</span><br><span class="line">    <span class="keyword">if</span> len(mention):</span><br><span class="line">        <span class="keyword">for</span> m <span class="keyword">in</span> mention:</span><br><span class="line">            mention_name = m.attrs[<span class="string">'title'</span>]</span><br><span class="line">            new_tag = soup.new_tag(<span class="string">'span'</span>)</span><br><span class="line">            new_tag.string = mention_name</span><br><span class="line">            m.replace_with(new_tag)</span><br><span class="line"></span><br><span class="line">    hashtag = soup.find_all(<span class="string">'e'</span>, attrs=&#123;<span class="string">'type'</span> : <span class="string">'hashtag'</span>&#125;)</span><br><span class="line">    <span class="keyword">if</span> len(hashtag):</span><br><span class="line">        <span class="keyword">for</span> tag <span class="keyword">in</span> hashtag:</span><br><span class="line">            tag_name = unquote(tag.attrs[<span class="string">'title'</span>])</span><br><span class="line">            new_tag = soup.new_tag(<span class="string">'span'</span>)</span><br><span class="line">            new_tag.string = tag_name</span><br><span class="line">            tag.replace_with(new_tag)</span><br><span class="line"></span><br><span class="line">    links = soup.find_all(<span class="string">'e'</span>, attrs=&#123;<span class="string">'type'</span> : <span class="string">'web'</span>&#125;)</span><br><span class="line">    <span class="keyword">if</span> len(links):</span><br><span class="line">        <span class="keyword">for</span> link <span class="keyword">in</span> links:</span><br><span class="line">            title = unquote(link.attrs[<span class="string">'title'</span>])</span><br><span class="line">            href = unquote(link.attrs[<span class="string">'href'</span>])</span><br><span class="line">            new_a_tag = soup.new_tag(<span class="string">'a'</span>, href=href)</span><br><span class="line">            new_a_tag.string = title</span><br><span class="line">            link.replace_with(new_a_tag)</span><br><span class="line"></span><br><span class="line">    text = str(soup)</span><br><span class="line">    text = re.sub(r<span class="string">'&lt;e[^&gt;]*&gt;'</span>, <span class="string">''</span>, text).strip()</span><br><span class="line">    text = text.replace(<span class="string">'\n'</span>, <span class="string">'&lt;br&gt;'</span>)</span><br><span class="line">    <span class="keyword">return</span> text</span><br><span class="line"></span><br><span class="line">def make_pdf(htmls):</span><br><span class="line">    html_files = []</span><br><span class="line">    <span class="keyword">for</span> index, html <span class="keyword">in</span> enumerate(htmls):</span><br><span class="line">        file = str(index) + <span class="string">".html"</span></span><br><span class="line">        html_files.append(file)</span><br><span class="line">        <span class="keyword">with</span> open(file, <span class="string">"w"</span>, encoding=<span class="string">"utf-8"</span>) <span class="keyword">as</span> f:</span><br><span class="line">            f.write(html)</span><br><span class="line"></span><br><span class="line">    options = &#123;</span><br><span class="line">        <span class="string">"user-style-sheet"</span>: <span class="string">"temp.css"</span>,</span><br><span class="line">        <span class="string">"page-size"</span>: <span class="string">"Letter"</span>,</span><br><span class="line">        <span class="string">"margin-top"</span>: <span class="string">"0.75in"</span>,</span><br><span class="line">        <span class="string">"margin-right"</span>: <span class="string">"0.75in"</span>,</span><br><span class="line">        <span class="string">"margin-bottom"</span>: <span class="string">"0.75in"</span>,</span><br><span class="line">        <span class="string">"margin-left"</span>: <span class="string">"0.75in"</span>,</span><br><span class="line">        <span class="string">"encoding"</span>: <span class="string">"UTF-8"</span>,</span><br><span class="line">        <span class="string">"custom-header"</span>: [(<span class="string">"Accept-Encoding"</span>, <span class="string">"gzip"</span>)],</span><br><span class="line">        <span class="string">"cookie"</span>: [</span><br><span class="line">            (<span class="string">"cookie-name1"</span>, <span class="string">"cookie-value1"</span>), (<span class="string">"cookie-name2"</span>, <span class="string">"cookie-value2"</span>)</span><br><span class="line">        ],</span><br><span class="line">        <span class="string">"outline-depth"</span>: <span class="number">10</span>,</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        pdfkit.from_file(html_files, PDF_FILE_NAME, options=options)</span><br><span class="line">    except Exception <span class="keyword">as</span> e:</span><br><span class="line">        pass</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> DELETE_HTML_WHEN_DONE:</span><br><span class="line">        <span class="keyword">for</span> file <span class="keyword">in</span> html_files:</span><br><span class="line">            os.remove(file)</span><br><span class="line"></span><br><span class="line">    print(<span class="string">"电子书生成成功！"</span>)</span><br><span class="line">def wordimage(content):</span><br><span class="line">    comments = <span class="string">''</span></span><br><span class="line">    <span class="keyword">for</span> k <span class="keyword">in</span> range(len(content)):</span><br><span class="line">        comments = comments + (str(content[k])).strip()</span><br><span class="line">    doc = PyQuery(comments)</span><br><span class="line">    #使用正则表达式去除标点符号</span><br><span class="line">    #pattern = re.compile(r'[\u4e00-\u9fa5]+')</span><br><span class="line">    #filterdata = re.findall(pattern, comments)</span><br><span class="line">    #cleaned_comments = ''.join(filterdata)</span><br><span class="line">    cleaned_comments = <span class="string">''</span>.join(jieba.cut(doc.text()))</span><br><span class="line">    print(cleaned_comments)</span><br><span class="line">    #使用结巴分词进行中文分词</span><br><span class="line">    segment = jieba.lcut(cleaned_comments)</span><br><span class="line">    words_df=pd.DataFrame(&#123;<span class="string">'segment'</span>:segment&#125;)</span><br><span class="line">    #去掉停用词，先下载https://github.com/wendy1990/short_text_classification/blob/master/conf/stopwords.txt</span><br><span class="line">    stopwords=pd.read_csv("stopwords.txt",index_col=False,quoting=3,sep="\t",names=['stopword'], encoding='utf-8')#quoting=3全不引用words_df=words_df[~words_df.segment.isin(stopwords.stopword)]</span><br><span class="line">    #统计词频 看看哪些词出现概率高</span><br><span class="line">    words_stat=words_df.groupby(by=[<span class="string">'segment'</span>])[<span class="string">'segment'</span>].agg(&#123;<span class="string">"计数"</span>:np.size&#125;)</span><br><span class="line">    words_stat=words_stat.reset_index().sort_values(by=[<span class="string">"计数"</span>],ascending=False)</span><br><span class="line">    #print(words_stat.head(1000).values)</span><br><span class="line">    stopwords = set(STOPWORDS)</span><br><span class="line">    #用词云进行显示</span><br><span class="line">    wordcloud=WordCloud(font_path=<span class="string">"c:\windos\fonts\simhei.ttf"</span>,background_color=<span class="string">"white"</span>,max_font_size=<span class="number">80</span>,stopwords=STOPWORDS.add(<span class="string">"said"</span>)).generate(cleaned_comments)</span><br><span class="line">    #word_frequence = &#123;x[0]:x[1] for x in words_stat.head(1000).values&#125;</span><br><span class="line">    #word_frequence_list = []</span><br><span class="line">    #for key in word_frequence:</span><br><span class="line">    #    temp = (key,word_frequence[key])</span><br><span class="line">    #    word_frequence_list.append(temp)</span><br><span class="line">    #wordcloud=wordcloud.fit_words(word_frequence_list)</span><br><span class="line">    #plt.imshow(wordcloud)</span><br><span class="line">    #plt.show()</span><br><span class="line">    plt.imshow(wordcloud, interpolation=<span class="string">"bilinear"</span>)</span><br><span class="line">    plt.axis(<span class="string">"off"</span>)</span><br><span class="line">    plt.show()</span><br><span class="line">    wordcloud.to_file(<span class="string">'zsxq_tool.jpg'</span>)</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    images_path = r<span class="string">'./images'</span></span><br><span class="line">    <span class="keyword">if</span> DOWLOAD_PICS:</span><br><span class="line">        <span class="keyword">if</span> os.path.exists(images_path):</span><br><span class="line">            shutil.rmtree(images_path)</span><br><span class="line">        os.mkdir(images_path)</span><br><span class="line"></span><br><span class="line">    # 仅精华</span><br><span class="line">    #start_url = 'https://api.zsxq.com/v1.10/groups/481818518558/topics?scope=digests&amp;count=30'</span><br><span class="line">    # 全部</span><br><span class="line">    #start_url = 'https://api.zsxq.com/v1.10/groups/481818518558/topics?count=30'</span><br><span class="line">    start_url = <span class="string">''</span></span><br><span class="line">    <span class="keyword">if</span> ONLY_DIGESTS:</span><br><span class="line">        start_url = <span class="string">'https://api.zsxq.com/v1.10/groups/'</span> + GROUP_ID + <span class="string">'/topics?scope=digests&amp;count='</span> + str(COUNTS_PER_TIME)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        start_url = <span class="string">'https://api.zsxq.com/v1.10/groups/'</span> + GROUP_ID + <span class="string">'/topics?scope=all&amp;count='</span> + str(COUNTS_PER_TIME)</span><br><span class="line"></span><br><span class="line">    url = start_url</span><br><span class="line">    <span class="keyword">if</span> FROM_DATE_TO_DATE and LATE_DATE.strip():</span><br><span class="line">        url = start_url + <span class="string">'&amp;end_time='</span> + quote(LATE_DATE.strip())</span><br><span class="line">    data = get_data(url)</span><br><span class="line">    #print(data[1])</span><br><span class="line">    #wordimage(data[1])</span><br><span class="line">    #make_pdf(data[0])</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> DOWLOAD_PICS and DELETE_PICS_WHEN_DONE:</span><br><span class="line">        shutil.rmtree(images_path)</span><br></pre></td></tr></table></figure>
<p>执行效果见图<br><img src="https://upload-images.jianshu.io/upload_images/17817191-07b0a8ce1442ea8a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>生成的PDF文件有点大，565页 ，50多M，主要是评论和图片都下载了，不下载的话5M差不多。</p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-87020a336d8967cf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>为了验证付费星球也能下载，我还建了个付费星球<a href="https://wx.zsxq.com/dweb2/index/group/224445125221，以后也会经常更新。" target="_blank" rel="noopener">https://wx.zsxq.com/dweb2/index/group/224445125221，以后也会经常更新。</a><br> <img src="https://upload-images.jianshu.io/upload_images/17817191-5f36737e35dbbff9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-aeca99f305f23d61.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>评论和回复也下载了<br><img src="https://upload-images.jianshu.io/upload_images/17817191-dc036a80532a5ce1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-70a44013887caa7d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>2019.6.19创建的星球<br><img src="https://upload-images.jianshu.io/upload_images/17817191-1818e57b45d5027b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>Python生成的词云效果不大好，还没过滤好无用的词。</p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-1a497d8234026372.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>文字版也导出到TXT了。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-a86943499b797fd1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>在词云网站 <a href="http://cloud.niucodata.com/" target="_blank" rel="noopener">http://cloud.niucodata.com/</a>  将下载的文字放进去就能看到词频统计和词云图。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-aace6c380516b87f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-ec6b0234c3353915.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>如果你想看下载这个PDF，公众号回复 <code>星球</code> 获取PDF和文字版。<br><a href="https://pan.baidu.com/s/1pJ-nLiLLMlK08_vwoE0ZuA" target="_blank" rel="noopener">https://pan.baidu.com/s/1pJ-nLiLLMlK08_vwoE0ZuA</a><br><a href="https://pan.baidu.com/s/1TcYpM4x5td2GLjkltHJhBQ" target="_blank" rel="noopener">https://pan.baidu.com/s/1TcYpM4x5td2GLjkltHJhBQ</a></p>
<h3 id="wkhtmltopdf"><a href="#wkhtmltopdf" class="headerlink" title="wkhtmltopdf"></a>wkhtmltopdf</h3><p>使用过程中发现wkhtmltopdf这个工具很好用 <a href="https://wkhtmltopdf.org/" target="_blank" rel="noopener">https://wkhtmltopdf.org/</a> 这个软件很方便在命令行生成图片和PDF，<br>执行 <code>wkhtmltoimage www.qq.com qq.png</code>，就会在当前目录下生成了一张png图片。<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">$ wkhtmltoimage www.qq.com qq.png</span><br><span class="line">Loading page (<span class="number">1</span>/<span class="number">2</span>)</span><br><span class="line">QFont::setPixelSize: Pixel size &lt;= <span class="number">0</span> (<span class="number">0</span>)                     ] <span class="number">44</span>%</span><br><span class="line">libpng warning: iCCP: known incorrect sRGB profile           ] <span class="number">64</span>%</span><br><span class="line">Rendering (<span class="number">2</span>/<span class="number">2</span>)</span><br><span class="line">Done</span><br><span class="line"></span><br><span class="line"> $ wkhtmltopdf www.qq.com qq.pdf</span><br><span class="line">Loading pages (<span class="number">1</span>/<span class="number">6</span>)</span><br><span class="line">QFont::setPixelSize: Pixel size &lt;= <span class="number">0</span> (<span class="number">0</span>)                     ] <span class="number">44</span>%</span><br><span class="line">libpng warning: iCCP: known incorrect sRGB profile===&gt;       ] <span class="number">87</span>%</span><br><span class="line">Counting pages (<span class="number">2</span>/<span class="number">6</span>)</span><br><span class="line">QFont::setPixelSize: Pixel size &lt;= <span class="number">0</span> (<span class="number">0</span>)=====================] <span class="built_in">Object</span> <span class="number">1</span> <span class="keyword">of</span> <span class="number">1</span></span><br><span class="line">Resolving links (<span class="number">4</span>/<span class="number">6</span>)</span><br><span class="line">Loading headers and footers (<span class="number">5</span>/<span class="number">6</span>)</span><br><span class="line">Printing pages (<span class="number">6</span>/<span class="number">6</span>)</span><br><span class="line">Done</span><br><span class="line">Warning: Received createRequest signal on a disposed ResourceObject<span class="string">'s NetworkAccessManager. This might be an indication of an iframe taking too long to load.</span></span><br><span class="line"><span class="string">Warning: Received createRequest signal on a disposed ResourceObject'</span>s NetworkAccessManager. This might be an indication <span class="keyword">of</span> an iframe taking too long to load.</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-b20148873f1dc5a0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>这个命令还可以增加一些参数，比如常用的设置宽高，图片质量等参数<br>执行<code>wkhtmltoimage --crop-w 410 --width 410 --quality 50 www.bing.com bing2.png</code>看看<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">--crop-w <span class="number">410</span>：截图宽度<span class="number">410</span>px</span><br><span class="line"></span><br><span class="line">--width <span class="number">410</span>：浏览器模拟宽度<span class="number">410</span>px</span><br><span class="line"></span><br><span class="line">--quality <span class="number">50</span>：图片质量(这个值越大，图片质量越高，当然文件也会比较大)</span><br><span class="line"></span><br><span class="line">还有更多参数用法，请 wkhtmltoimage -h查看。</span><br><span class="line">HTML转pdf同理，wkhtmltopdf -h</span><br><span class="line">wkhtmltopdf -v</span><br><span class="line">--collate             当输出多个副本时进行校验（这是默认设置）</span><br><span class="line">    --no-collate          当输出多个副本时不进行校验</span><br><span class="line">    --cookie-jar &lt;path&gt;   从提供的 JAR 文件中读写 cookie 数据</span><br><span class="line">    --copies &lt;number&gt;     设置输出副本的数量（默认主 <span class="number">1</span>)，其实为 <span class="number">1</span> 就够了</span><br><span class="line">-d, --dpi &lt;dpi&gt;           指定一个要分辨率（这在 X11 系统中并没有什么卵用）</span><br><span class="line">-H, --extended-help       相对 -h 参数，显示更详细的说明文档</span><br><span class="line">-g, --grayscale           指定以灰度图生成 PDF 文档。占用的空间更小</span><br><span class="line">-h, --help                显示帮助信息</span><br><span class="line">    --htmldoc             输出程序的 html 帮助文档</span><br><span class="line">    --image-dpi &lt;integer&gt; 当页面中有内嵌的图片时，</span><br><span class="line">                          会下载此命令行参数指定尺寸的图片（默认值是 <span class="number">600</span>)</span><br><span class="line">    --image-quality &lt;interger&gt; 当使用 jpeg 算法压缩图片时使用这个参数指定的质量（默认为 <span class="number">94</span>)</span><br><span class="line">    --license             输出授权信息并退出</span><br><span class="line">-l, --lowquality          生成低质量的 PDF/PS , 能够很好的节约最终生成文档所占存储空间</span><br><span class="line">    --manpage             输出程序的手册页</span><br><span class="line">-B, --margin-bottom &lt;unitreal&gt; 设置页面的 底边距</span><br><span class="line">-L, --margin-left &lt;unitreal&gt;   设置页面的 左边距 （默认是 <span class="number">10</span>mm)</span><br><span class="line">-R, --margin-right &lt;unitreal&gt;  设置页面的 右边距 （默认是 <span class="number">10</span>mm)</span><br><span class="line">-T, --margin-top &lt;unitreal&gt;    设置页面的 上边距</span><br><span class="line">-O, --orientation &lt;orientation&gt; 设置为“风景 (Landscape)”或“肖像 (Portrait)”模式，</span><br><span class="line">                                默认是肖像模块 (Portrait)</span><br><span class="line">    --page-height &lt;unitreal&gt;   页面高度</span><br><span class="line">-s, --page-size &lt;Size&gt;         设置页面的尺寸，如：A4,Letter 等，默认是：A4</span><br><span class="line">    --page-width &lt;unitreal&gt;    页面宽度</span><br><span class="line">    --no-pdf-compression       不对 PDF 对象使用丢失少量信息的压缩算法，不建议使用些参数，</span><br><span class="line">                               因为生成的 PDF 文件会非常大。</span><br><span class="line">-q, --quiet                    静态模式，不在标准输出中打印任何信息</span><br><span class="line">    --read-args-<span class="keyword">from</span>-stdin     从标准输入中读取命令行参数，后续会有针对此指令的详细介绍，</span><br><span class="line">                               请参见 **从标准输入获取参数**</span><br><span class="line">    --readme                   输出程序的 readme 文档</span><br><span class="line">    --title &lt;text&gt;             生成的 PDF 文档的标题，如果不指定则使用第一个文档的标题</span><br><span class="line">-V, --version                  输出版本信息后退出</span><br></pre></td></tr></table></figure></p>
<p>wkhtmltopdf “/home/wang/test.html” /home/wang/test.pdf</p>
<p>wkhtmltopdf “<a href="https://www.jd.com&quot;" target="_blank" rel="noopener">https://www.jd.com&quot;</a> /home/wang/jj.pdf</p>
<h3 id="中文乱码"><a href="#中文乱码" class="headerlink" title="中文乱码"></a>中文乱码</h3><p>如果你是在 linux 上执行脚本可能会出现中文乱码，解决方法就是从windows拷贝宋体字体文件 <code>c:\windos\fonts\simhei.ttf</code>到<code>/usr/share/fonts/</code>下<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">cd /usr/share/fonts/</span><br><span class="line">cp simhei.ttf .</span><br><span class="line">mkfontscale</span><br><span class="line">mkfontdir</span><br><span class="line">fc-cache</span><br></pre></td></tr></table></figure></p>
<p>再次执行fc-list可以看到已经安装的字体了。</p>
<h3 id="phantomjs"><a href="#phantomjs" class="headerlink" title="phantomjs"></a>phantomjs</h3><p>除了wkhtmltopdf生成PDF还有phantomjs 的功能也很强大，做爬虫应用，抓取网页数据、网页截屏、页面访问自动化等。<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">set_time_limit(<span class="number">0</span>);</span><br><span class="line">$path = <span class="string">'/bin/phantomjs'</span>;		<span class="comment">//phantomjs路径</span></span><br><span class="line"><span class="comment">//$jsPath = '/home/test/phantomjs/examples/test.js';	</span></span><br><span class="line">$url = <span class="string">'https://baidu.com/'</span>;		<span class="comment">//要抓取的网页</span></span><br><span class="line">$out = <span class="string">'baidu.png'</span>;			<span class="comment">//图片保存路径</span></span><br><span class="line">$cmd = <span class="string">"$path --output-encoding=utf-8 $jsPath $url $out"</span>;</span><br><span class="line">echo $cmd;</span><br><span class="line">system($cmd);</span><br><span class="line"></span><br><span class="line">创建test.js</span><br><span class="line"><span class="string">``</span><span class="string">`js</span></span><br><span class="line"><span class="string">var page = require('webpage').create();</span></span><br><span class="line"><span class="string">var args = require('system').args;</span></span><br><span class="line"><span class="string">var url = args[1];</span></span><br><span class="line"><span class="string">var filename = args[2];</span></span><br><span class="line"><span class="string">page.open(url, function () &#123;</span></span><br><span class="line"><span class="string">    page.render(filename);</span></span><br><span class="line"><span class="string">    phantom.exit();</span></span><br><span class="line"><span class="string">&#125;);</span></span><br></pre></td></tr></table></figure></p>
<p>执行 /home/test/phantomjs/bin/phantomjs  test.js   <a href="http://www.baidu.com" target="_blank" rel="noopener">http://www.baidu.com</a>   baidu.png</p>
<h3 id="puppeteer"><a href="#puppeteer" class="headerlink" title="puppeteer"></a>puppeteer</h3><p>puppeteer 是 chrome 提供的一个无头浏览器，它是替代 phantomjs 的一个替代品，多用于实现自动化测试。官方仓库地址：<a href="https://github.com/GoogleChrome/puppeteer,Puppeteer" target="_blank" rel="noopener">https://github.com/GoogleChrome/puppeteer,Puppeteer</a> 能用来干啥，有哪些功能：<br>生成页面的截图和 PDF<br>爬取网站内容<br>模拟登陆、自动提交表单，UI 测试，键盘输入等<br>使用最新的 JavaScript 和浏览器功能，直接在最新版本的 Chrome 中运行测试。<br>捕获网站的时间线跟踪，以帮助诊断性能问题<br>测试 Chrome 扩展<br>screenshoteer是基于Puppeteer 的命令行工具<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">npm i -g screenshoteer</span><br><span class="line">screenshoteer --url https:<span class="comment">//nicelinks.site/</span></span><br><span class="line">λ screenshoteer --url http:<span class="comment">//qq.com</span></span><br><span class="line">http:<span class="comment">//qq.com</span></span><br><span class="line"><span class="literal">true</span></span><br><span class="line">腾讯首页</span><br></pre></td></tr></table></figure></p>
<h3 id="pdfkit"><a href="#pdfkit" class="headerlink" title="pdfkit"></a>pdfkit</h3><p>这是个Python生成PDF的库<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; <span class="keyword">import</span> pdfkit</span><br><span class="line">&gt;&gt;&gt; config = pdfkit.configuration(wkhtmltopdf=r<span class="string">'d:\wkhtmltopdf\bin\wkhtmltopdf.exe'</span>)</span><br><span class="line">&gt;&gt;&gt; pdfkit.from_url(<span class="string">'https://sogou.com'</span>, <span class="string">'sogou.pdf'</span>, configuration=config)</span><br><span class="line">Loading pages (<span class="number">1</span>/<span class="number">6</span>)</span><br><span class="line">Counting pages (<span class="number">2</span>/<span class="number">6</span>)</span><br><span class="line">Resolving links (<span class="number">4</span>/<span class="number">6</span>)</span><br><span class="line">Loading headers and footers (<span class="number">5</span>/<span class="number">6</span>)</span><br><span class="line">Printing pages (<span class="number">6</span>/<span class="number">6</span>)</span><br><span class="line">Done</span><br><span class="line">True</span><br></pre></td></tr></table></figure></p>
<p>也可以将本地文件生成PDF<br> pdfkit.from_file(‘test.html’, r’D:\test’ + id + ‘.pdf’, configuration=config)</p>
<h3 id="popple"><a href="#popple" class="headerlink" title="popple"></a>popple</h3><p>pdf软件 poppler  <a href="https://github.com/freedesktop/poppler" target="_blank" rel="noopener">https://github.com/freedesktop/poppler</a><br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">$ pdfinfo   知识星球_苏生不惑.pdf</span><br><span class="line">Title:</span><br><span class="line">Creator:        wkhtmltopdf <span class="number">0.12</span><span class="number">.5</span></span><br><span class="line">Producer:       Qt <span class="number">4.8</span><span class="number">.7</span></span><br><span class="line">CreationDate:   <span class="number">09</span>/<span class="number">04</span>/<span class="number">19</span> <span class="number">21</span>:<span class="number">02</span>:<span class="number">59</span> </span><br><span class="line">Tagged:         no</span><br><span class="line">UserProperties: no</span><br><span class="line">Suspects:       no</span><br><span class="line">Form:           none</span><br><span class="line">JavaScript:     no</span><br><span class="line">Pages:          <span class="number">565</span></span><br><span class="line">Encrypted:      no</span><br><span class="line">Page size:      <span class="number">612</span> x <span class="number">792</span> pts (letter)</span><br><span class="line">Page rot:       <span class="number">0</span></span><br><span class="line">File size:      <span class="number">0</span> bytes</span><br><span class="line">Optimized:      no</span><br><span class="line">PDF version:    <span class="number">1.4</span></span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">λ pdf</span><br><span class="line">pdfdetach.exe    pdfimages.exe    pdfseparate.exe  pdftohtml.exe    pdftops.exe      pdfunite.exe</span><br><span class="line">pdffonts.exe     pdfinfo.exe      pdftocairo.exe   pdftoppm.exe     pdftotext.exe</span><br></pre></td></tr></table></figure></p>
<h3 id="资源"><a href="#资源" class="headerlink" title="资源"></a>资源</h3><p>pdf合并工具pdftk <a href="https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/" target="_blank" rel="noopener">https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/</a><br>  <a href="https://segmentfault.com/a/1190000018765575" target="_blank" rel="noopener">初识wkhtmltopdf</a><br>中文字体<a href="https://github.com/sonatype/maven-guide-zh/raw/master/content-zh/src/main/resources/fonts/simsun.ttc" target="_blank" rel="noopener">https://github.com/sonatype/maven-guide-zh/raw/master/content-zh/src/main/resources/fonts/simsun.ttc</a><br><a href="http://einverne.github.io/post/2019/01/html-to-pdf.html" target="_blank" rel="noopener">http://einverne.github.io/post/2019/01/html-to-pdf.html</a><br><a href="https://hacpai.com/article/1544237764458" target="_blank" rel="noopener"> Web 屏幕截图工具</a><br>使用 Laravel-snappy 生成 PDF 踩坑记录<a href="https://learnku.com/articles/25922" target="_blank" rel="noopener">https://learnku.com/articles/25922</a><br> <a href="https://github.com/Kozea/WeasyPrint" target="_blank" rel="noopener">https://github.com/Kozea/WeasyPrint</a><br>基于前端技术生成PDF方案<a href="https://juejin.im/post/5d036a78f265da1bce3dcd40" target="_blank" rel="noopener">https://juejin.im/post/5d036a78f265da1bce3dcd40</a><br> <a href="https://segmentfault.com/a/1190000018701596" target="_blank" rel="noopener">HTML转成PDF的4个方案及实现</a><br>js导出图片与pdf的方案<a href="https://www.jianshu.com/p/408748cfa76e" target="_blank" rel="noopener">https://www.jianshu.com/p/408748cfa76e</a><br> <a href="https://brianshen1990.github.io/HTML_To_PDF_Solutions.html" title="Permalink to HTML To PDF Solutions / HTML 转换 PDF 解决方案" target="_blank" rel="noopener">HTML 转换 PDF 解决方案</a><br>抓取网页生成 PDF <a href="http://jartto.wang/2018/10/13/nodejs-pdf/" target="_blank" rel="noopener">http://jartto.wang/2018/10/13/nodejs-pdf/</a></p>
<h3 id="星球导航网站"><a href="#星球导航网站" class="headerlink" title="星球导航网站"></a>星球导航网站</h3><p><a href="https://tooltowin.com/zsxq/1124/" target="_blank" rel="noopener">https://tooltowin.com/zsxq/1124/</a><br><a href="https://www.101ker.com/p/2284" target="_blank" rel="noopener">https://www.101ker.com/p/2284</a><br> <a href="https://xiaogenhua.com/2019/05/20/caozsay/" target="_blank" rel="noopener">https://xiaogenhua.com/2019/05/20/caozsay/</a><br><a href="https://www.kindkp.com/" target="_blank" rel="noopener">https://www.kindkp.com/</a> </p>
<h3 id="推荐星球"><a href="#推荐星球" class="headerlink" title="推荐星球"></a>推荐星球</h3><p>这段时间也加入了不少星球，推荐一个我几乎每天看的星球，为什么每天看呢，因为星主每天分享，太勤快了，星球名叫 <code>风巢套利日享（限免）</code>，免费的<a href="https://wx.zsxq.com/dweb2/index/group/554228114224" target="_blank" rel="noopener">https://wx.zsxq.com/dweb2/index/group/554228114224</a><br>这是这个星球的词云，看到这些关键词，你心动了吗？点击阅读原文或扫码加入星球。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-2211005aef836d43.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-5d2c9bc76a82c136.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>路人甲<br><img src="https://upload-images.jianshu.io/upload_images/17817191-410eb89d1155c975.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>不安分的<br><img src="https://upload-images.jianshu.io/upload_images/17817191-2b1906c35e8a37da.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>大门口<br><img src="https://upload-images.jianshu.io/upload_images/17817191-5bd614a2d0559d20.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>最后如果你也想导出哪个星球，拉我进去帮你导出来。</p>
<p>推荐阅读:</p>
<p><a href="https://mp.weixin.qq.com/s/MUIPj4OgSxAeRl5Hk-2tuw" target="_blank" rel="noopener">没有提取码怎么获取百度网盘资源？</a></p>
<p><a href="https://mp.weixin.qq.com/s/Xz1m-mqtCcBF_4hmGCpkUQ" target="_blank" rel="noopener">如何发一条空白的朋友圈</a></p>
<p><a href="https://mp.weixin.qq.com/s/_3AeNahwbs8c3UJ0is1t4A" target="_blank" rel="noopener">如何在电脑上登陆多个微信</a></p>
<p><a href="https://mp.weixin.qq.com/s/qyFvOgHf1mXwPKO0tQwUyg" target="_blank" rel="noopener">如何提取公积金 9 天到账</a></p>
<p><a href="https://mp.weixin.qq.com/s/1omFkK5PPyeJEzUTagj9qg" target="_blank" rel="noopener">免费在线听周杰伦歌曲</a></p>
<p><a href="https://mp.weixin.qq.com/s/eGDO0Y8el_dsEyriCoAgog" target="_blank" rel="noopener">那些你可能不知道的微信奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/k5lBwiDzGgSU3fh2v2Rw9A" target="_blank" rel="noopener">如何在豆瓣租房小组快速找到满意的房子</a></p>
<p><a href="https://mp.weixin.qq.com/s/sng3uK9Nge1OD2gc5DDuZg" target="_blank" rel="noopener">那些你可能用得上的简历写作工具</a></p>
<p><a href="https://mp.weixin.qq.com/s/adJFh_9LH0N-vvvYaiQqXg" target="_blank" rel="noopener">Chrome 浏览器扩展神器油猴</a></p>
<p><a href="https://mp.weixin.qq.com/s/mI5kubVY2t5jwJ9ub7A1iA" target="_blank" rel="noopener">我的新浪工作日常</a></p>
<h3 id="公众号：苏生不惑"><a href="#公众号：苏生不惑" class="headerlink" title="公众号：苏生不惑"></a>公众号：苏生不惑</h3><p> <img src="https://upload-images.jianshu.io/upload_images/17817191-6e0079f95d4c0338.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="扫描二维码关注"></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/09/16/那些你可能不知道的-bilibili-奇技淫巧/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/09/16/那些你可能不知道的-bilibili-奇技淫巧/" itemprop="url">那些你可能不知道的 bilibili 奇技淫巧</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-16T20:28:12+08:00">
                2019-09-16
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2.2k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  9 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>这是奇技淫巧系列第9篇文章了。</p>
<p><a href="https://mp.weixin.qq.com/s/-cSjrvkibYGp5Fx8gCTFuw" target="_blank" rel="noopener">那些你可能不知道的浏览器奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/sqRgMh4rxFBt5YxNtaa6dw" target="_blank" rel="noopener">那些你可能不知道的知乎奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/eGDO0Y8el_dsEyriCoAgog" target="_blank" rel="noopener">那些你可能不知道的微信奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/j7VhoZXmUTnOWC5C_B8jlQ" target="_blank" rel="noopener">那些你可能不知道的微博奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/LtI2piwAIDXA590NEsXvuw" target="_blank" rel="noopener">那些你可能不知道的网易云音乐奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/-5tZWfeWWa_E8jRCH0T_Cw" target="_blank" rel="noopener">那些你可能不知道的搜索奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/5InOxKmi9eXk33G_8N3byQ" target="_blank" rel="noopener">那些你可能不知道的视频下载奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/R3x-xZwqLIVwPjlgikDQ9A" target="_blank" rel="noopener">那些你可能不知道的免费观看 VIP 视频奇技淫巧</a></p>
<p>b站在国内视频网站是一股清流，一个看视频不用等60s广告的网站，今天就说说这个二次元网站的一些实用工具/技巧。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-734c02d1fb334e94.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="哔哩哔哩助手"><a href="#哔哩哔哩助手" class="headerlink" title="哔哩哔哩助手"></a>哔哩哔哩助手</h3><p><a href="https://github.com/bilibili-helper/bilibili-helper" target="_blank" rel="noopener">https://github.com/bilibili-helper/bilibili-helper</a>  这是一个b站 辅助工具 <a href="https://bilibili-helper.github.io/" target="_blank" rel="noopener">https://bilibili-helper.github.io/</a> ，可以替换播放器、去广告、推送通知并进行一些快捷操作。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-9d41b316c481ade0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>Chrome扩展下载链接 <a href="https://chrome.google.com/webstore/detail/%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9%E5%8A%A9%E6%89%8B%EF%BC%9Abilibilicom-%E7%BB%BC%E5%90%88%E8%BE%85%E5%8A%A9%E6%89%A9%E5%B1%95/kpbnombpnpcffllnianjibmpadjolanh" target="_blank" rel="noopener">https://chrome.google.com/webstore/detail/%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9%E5%8A%A9%E6%89%8B%EF%BC%9Abilibilicom-%E7%BB%BC%E5%90%88%E8%BE%85%E5%8A%A9%E6%89%A9%E5%B1%95/kpbnombpnpcffllnianjibmpadjolanh</a></p>
<p>比如这个视频 <a href="https://www.bilibili.com/video/av10914119" target="_blank" rel="noopener">https://www.bilibili.com/video/av10914119</a> ，安装扩展登录b站后右侧有个助手按钮，可以直接下载当前播放的视频和弹幕。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-5224a2893e01864b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>更多功能可以自己设置<br><img src="https://upload-images.jianshu.io/upload_images/17817191-fd158e3deba3e186.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="观看港澳台番剧"><a href="#观看港澳台番剧" class="headerlink" title="观看港澳台番剧"></a>观看港澳台番剧</h3><p>有些番剧限制了港澳台才能看，比如 <a href="https://www.bilibili.com/bangumi/play/ss28049" target="_blank" rel="noopener">https://www.bilibili.com/bangumi/play/ss28049</a> 这个会提示无法观看。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-f724ce76cb2b423e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>这时候就需要使用油猴了，之前写过怎么安装油猴 <a href="https://mp.weixin.qq.com/s/adJFh_9LH0N-vvvYaiQqXg" target="_blank" rel="noopener">Chrome 浏览器扩展神器油猴</a></p>
<p> 安装这个脚本 <a href="https://greasyfork.org/zh-CN/scripts/25718" target="_blank" rel="noopener">https://greasyfork.org/zh-CN/scripts/25718</a> ，有问题查看这里 <a href="https://github.com/ipcjs/bilibili-helper/blob/user.js/bilibili_bangumi_area_limit_hack.md" target="_blank" rel="noopener">https://github.com/ipcjs/bilibili-helper/blob/user.js/bilibili_bangumi_area_limit_hack.md</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-aa112a8acd69bc1a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>安装脚本后就可以愉快的看这个视频了，页面上有些文字也成繁体了。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-c6319b994ddf2110.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>ps:如果你是用的Windowns 10 ，可以在window应用市场里下载UWP版本的bilibili，就可以直接看了。</p>
<h3 id="视频下载"><a href="#视频下载" class="headerlink" title="视频下载"></a>视频下载</h3><p>可以直接在<a href="https://www.ibilibili.com/" target="_blank" rel="noopener">https://www.ibilibili.com/</a> 输入播放地址，或者直接改为 <a href="https://www.ibilibili.com/video/av10914119" target="_blank" rel="noopener">https://www.ibilibili.com/video/av10914119</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-a18c42aa8088286f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>对应的还有 <a href="https://www.jijidown.com/video/av10914119" target="_blank" rel="noopener">https://www.jijidown.com/video/av10914119</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-72a34be095d56285.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>还有人开发了客户端版本下载工具，公众号回复 <code>b站</code>获取。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-f46337e0be7de8d1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>当然我更推荐使用命令行来下载<a href="https://mp.weixin.qq.com/s/5InOxKmi9eXk33G_8N3byQ" target="_blank" rel="noopener">那些你可能不知道的视频下载奇技淫巧</a><br><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"> you-get -i https:<span class="comment">//www.bilibili.com/video/av36043614</span></span><br><span class="line">site:                Bilibili</span><br><span class="line">title:               一个MV 周杰伦《晴天》</span><br><span class="line">streams:             # Available quality and codecs</span><br><span class="line">    [ DASH ] ____________________________________</span><br><span class="line">    - format:        dash-flv720</span><br><span class="line">      container:     mp4</span><br><span class="line">      quality:       高清 <span class="number">720</span>P</span><br><span class="line">      size:          <span class="number">80.8</span> MiB (<span class="number">84735780</span> bytes)</span><br><span class="line">    # download-with: you-get --format=dash-flv720 [URL]</span><br><span class="line"></span><br><span class="line">    - format:        dash-flv480</span><br><span class="line">      container:     mp4</span><br><span class="line">      quality:       清晰 <span class="number">480</span>P</span><br><span class="line">      size:          <span class="number">42.4</span> MiB (<span class="number">44437476</span> bytes)</span><br><span class="line">    # download-with: you-get --format=dash-flv480 [URL]</span><br><span class="line"></span><br><span class="line">    - format:        dash-flv360</span><br><span class="line">      container:     mp4</span><br><span class="line">      quality:       流畅 <span class="number">360</span>P</span><br><span class="line">      size:          <span class="number">16.3</span> MiB (<span class="number">17131306</span> bytes)</span><br><span class="line">    # download-with: you-get --format=dash-flv360 [URL]</span><br><span class="line"></span><br><span class="line">    [ DEFAULT ] _________________________________</span><br><span class="line">    - format:        flv720</span><br><span class="line">      container:     flv</span><br><span class="line">      quality:       高清 <span class="number">720</span>P</span><br><span class="line">      size:          <span class="number">81.0</span> MiB (<span class="number">84952120</span> bytes)</span><br><span class="line">    # download-with: you-get --format=flv720 [URL]</span><br><span class="line"></span><br><span class="line">    - format:        flv480</span><br><span class="line">      container:     flv</span><br><span class="line">      quality:       清晰 <span class="number">480</span>P</span><br><span class="line">      size:          <span class="number">42.6</span> MiB (<span class="number">44653816</span> bytes)</span><br><span class="line">    # download-with: you-get --format=flv480 [URL]</span><br><span class="line"></span><br><span class="line">    - format:        flv360</span><br><span class="line">      container:     flv</span><br><span class="line">      quality:       流畅 <span class="number">360</span>P</span><br><span class="line">      size:          <span class="number">16.5</span> MiB (<span class="number">17347646</span> bytes)</span><br><span class="line">    # download-with: you-get --format=flv360 [URL]</span><br></pre></td></tr></table></figure></p>
<h3 id="B站封面提取"><a href="#B站封面提取" class="headerlink" title="B站封面提取"></a>B站封面提取</h3><p>每个视频都有封面图片，可以在 <a href="http://www.galmoe.com/" target="_blank" rel="noopener">http://www.galmoe.com/</a> 输入 id号获取，比如 <a href="https://www.bilibili.com/video/av10914119" target="_blank" rel="noopener">https://www.bilibili.com/video/av10914119</a> 这个视频，输入 av10914119  获取到封面<a href="https://i2.hdslb.com/bfs/archive/6ca1b6647b4b97221ef82fec7148eee4920db8fe.png" target="_blank" rel="noopener">https://i2.hdslb.com/bfs/archive/6ca1b6647b4b97221ef82fec7148eee4920db8fe.png</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-5425a1f8e7af8d9d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-2e10710fd0835bec.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>除了网站还有对应的Chrome扩展 <a href="https://github.com/jsososo/bilibili-hand-in-the-cover" target="_blank" rel="noopener">https://github.com/jsososo/bilibili-hand-in-the-cover</a></p>
<h3 id="TOP-100-UP-主"><a href="#TOP-100-UP-主" class="headerlink" title="TOP 100 UP 主"></a>TOP 100 UP 主</h3><p><a href="http://rank.python666.cn/" target="_blank" rel="noopener">http://rank.python666.cn/</a> 这里可以看到UP 主粉丝 TOP 100 榜单，papi酱 我偶尔也看看。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-acc896ef999f6191.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="kanbilibili"><a href="#kanbilibili" class="headerlink" title="kanbilibili"></a>kanbilibili</h3><p>这是一个基于b站的第三方网站，bilibili 网址前面加上 kan <a href="https://www.kanbilibili.com/" target="_blank" rel="noopener">https://www.kanbilibili.com/</a>  ，会定期发布 日报、周刊、月刊、bilibili总榜、Up主排行榜 等数据统计报告，视频详情页提供 历史数据查看、B站视频下载、B站视频封面下载 等功能。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-02e73bf57b2fa7ad.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>比如上面的视频在这里的地址就是<a href="https://www.kanbilibili.com/video/av10914119" target="_blank" rel="noopener">https://www.kanbilibili.com/video/av10914119</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-d209b214dbb0cb88.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>可以看到UP主排行榜，每周五更新  <a href="https://www.kanbilibili.com/rank/ups/fans" target="_blank" rel="noopener">https://www.kanbilibili.com/rank/ups/fans</a> ，我关注的papi酱有537万粉丝，播放数有3个多亿。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-20c8e617fe3b74d7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>还有视频转mp3功能，不过还是建议使用 ffmpeg 在命令行转换，可以看我之前的文章<a href="https://mp.weixin.qq.com/s/624Hv1krGUboecz1QX8O1g" target="_blank" rel="noopener">ffmpeg 将视频转GIF</a></p>
<h3 id="粉丝在线统计"><a href="#粉丝在线统计" class="headerlink" title="粉丝在线统计"></a>粉丝在线统计</h3><p>比如输入papi酱 <a href="https://space.bilibili.com/1532165/" target="_blank" rel="noopener">https://space.bilibili.com/1532165/</a> 的ID 1532165 可以看到具体的粉丝数，而不是b站上的538万。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-9e9442e75f600a5a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-ad6adabe77a3fe2e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="b站观测者"><a href="#b站观测者" class="headerlink" title="b站观测者"></a>b站观测者</h3><p><a href="https://www.biliob.com" target="_blank" rel="noopener">https://www.biliob.com</a> 这个网站的目的是搜集并观测B站的UP主、视频或番剧等数据。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-acc0e717aec72ca9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="直播录制工具"><a href="#直播录制工具" class="headerlink" title="直播录制工具"></a>直播录制工具</h3><p>B站录播姬直接下载直播流，经过处理后保存到硬盘。支持自动根据文件大小或录制时长切割视频文件，支持只保存即时回放剪辑<a href="https://rec.danmuji.org/" target="_blank" rel="noopener">https://rec.danmuji.org/</a></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-9f9fa0c5d8977f75.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>对应的还有直播弹幕工具 <a href="https://www.danmuji.org/" target="_blank" rel="noopener">https://www.danmuji.org/</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-9ca30e82ca954bff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="增强脚本"><a href="#增强脚本" class="headerlink" title="增强脚本"></a>增强脚本</h3><p>强大的哔哩哔哩增强脚本: 下载视频, 音乐, 封面, 弹幕 / 自定义播放器画质, 模式, 布局 / 自定义顶栏, 删除广告, 夜间模式 / 触屏设备支持<a href="https://github.com/the1812/Bilibili-Evolved，需要浏览器安装Tampermonkey插件。" target="_blank" rel="noopener">https://github.com/the1812/Bilibili-Evolved，需要浏览器安装Tampermonkey插件。</a></p>
<p>各种视频相关功能有：<br><img src="https://upload-images.jianshu.io/upload_images/17817191-4bfc9707d0b480e7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-27d745f8a7745def.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="bilibili视频编辑器"><a href="#bilibili视频编辑器" class="headerlink" title="bilibili视频编辑器"></a>bilibili视频编辑器</h3><p>bilibili视频编辑器是一款线上视频编辑工具，无需下载，直接在网页浏览器中进行操作 ，具体使用见<a href="https://www.bilibili.com/blackboard/help.html#bilibili%E8%A7%86%E9%A2%91%E7%BC%96%E8%BE%91%E5%99%A8?id=c4b1536079b04e5cbeb5a35606ea5137" target="_blank" rel="noopener">https://www.bilibili.com/blackboard/help.html#bilibili%E8%A7%86%E9%A2%91%E7%BC%96%E8%BE%91%E5%99%A8?id=c4b1536079b04e5cbeb5a35606ea5137</a><br>下载地址 <a href="https://bilibili.clipchamp.com/" target="_blank" rel="noopener">https://bilibili.clipchamp.com/</a>，需要使用b站账号登录 ，只支持Chrome浏览器。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-e5bf0fee5d4cd234.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-ca8ca972f1acd552.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><code>如果文章对你有帮助，还望不吝点个在看支持下我写下去的动力。</code></p>
<h3 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h3><p>小丸工具箱<a href="https://maruko.appinn.me/" target="_blank" rel="noopener">https://maruko.appinn.me/</a><br>视频下载<a href="https://toutiao.iiilab.com/" target="_blank" rel="noopener">https://toutiao.iiilab.com/</a><br>油猴脚本 解析各大视频网站VIP视频<a href="https://github.com/syhyz1990/media/" target="_blank" rel="noopener">https://github.com/syhyz1990/media/</a><br><a href="https://www.biliplus.com/" target="_blank" rel="noopener">https://www.biliplus.com/</a><br> <a href="https://go2think.com/bilibili-nolimit/" target="_blank" rel="noopener">如何解除B站</a><br>观看港澳台番剧 <a href="https://zhaoolee.com/ChromeAppHeroes/page/016_jie_chu_b_zhan_qu_yu_xian_zhi.html" target="_blank" rel="noopener">https://zhaoolee.com/ChromeAppHeroes/page/016_jie_chu_b_zhan_qu_yu_xian_zhi.html</a><br>助手<a href="https://zhaoolee.com/ChromeAppHeroes/page/021_bi_li_bi_li_zhu_shou.html" target="_blank" rel="noopener">https://zhaoolee.com/ChromeAppHeroes/page/021_bi_li_bi_li_zhu_shou.html</a><br>bilibili 视频下载 油猴插件<a href="https://github.com/evgo2017/bilibili_video_download" target="_blank" rel="noopener">https://github.com/evgo2017/bilibili_video_download</a><br>强大的哔哩哔哩增强脚本<a href="https://github.com/the1812/Bilibili-Evolved" target="_blank" rel="noopener">https://github.com/the1812/Bilibili-Evolved</a><br><a href="https://b23.tv/av58983681" target="_blank" rel="noopener">https://b23.tv/av58983681</a><br>B站2000万用户分析<a href="https://zhuanlan.zhihu.com/p/24434456" target="_blank" rel="noopener">https://zhuanlan.zhihu.com/p/24434456</a><br>Bilibili 用户爬虫<a href="https://github.com/airingursb/bilibili-user" target="_blank" rel="noopener">https://github.com/airingursb/bilibili-user</a> <a href="http://ursb.me/bilibili-report/" target="_blank" rel="noopener">http://ursb.me/bilibili-report/</a><br>抓取了BiliBili能获取哪些数据？<a href="https://www.jianshu.com/p/1c263f39e68a" target="_blank" rel="noopener">https://www.jianshu.com/p/1c263f39e68a</a><br>Bilibili 数据接口整理<a href="https://juejin.im/entry/58636c58128fe1006bc5588b" target="_blank" rel="noopener">https://juejin.im/entry/58636c58128fe1006bc5588b</a><br>Python爬取bilibili全站用户信息<a href="https://juejin.im/post/5b8fe524e51d450e531c3dcd" target="_blank" rel="noopener">https://juejin.im/post/5b8fe524e51d450e531c3dcd</a><br>b站视频数据播报 <a href="https://sharecuts.cn/shortcut/1068" target="_blank" rel="noopener">https://sharecuts.cn/shortcut/1068</a><br>B站视频数据分析报告<a href="http://z_plus.coding.me/" target="_blank" rel="noopener">http://z_plus.coding.me/</a><br>b站指数<a href="http://www.gsdata.cn/rank/morerank?v_type=bilibili" target="_blank" rel="noopener">http://www.gsdata.cn/rank/morerank?v_type=bilibili</a><br><a href="https://wuyin.io/2018/03/31/bilibili-live-crawler-and-auto-edit-recording/" target="_blank" rel="noopener">https://wuyin.io/2018/03/31/bilibili-live-crawler-and-auto-edit-recording/</a><br>如何免费采集哔哩哔哩视频数据<a href="http://www.houyicaiji.com/?type=post&amp;pid=3812" target="_blank" rel="noopener">http://www.houyicaiji.com/?type=post&amp;pid=3812</a><br><a href="https://github.com/AInoob/bzhua" target="_blank" rel="noopener">https://github.com/AInoob/bzhua</a><br><a href="http://www.bilijk.tk/" target="_blank" rel="noopener">http://www.bilijk.tk/</a><br> A站B站数据分析实战<a href="http://www.woshipm.com/data-analysis/1622826.html" target="_blank" rel="noopener">http://www.woshipm.com/data-analysis/1622826.html</a><br>bilibili哔哩哔哩下载助手<a href="https://chrome.google.com/webstore/detail/bilibili%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9%E4%B8%8B%E8%BD%BD%E5%8A%A9%E6%89%8B/bfcbfobhcjbkilcbehlnlchiinokiijp?hl=zh-CN" target="_blank" rel="noopener">https://chrome.google.com/webstore/detail/bilibili%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9%E4%B8%8B%E8%BD%BD%E5%8A%A9%E6%89%8B/bfcbfobhcjbkilcbehlnlchiinokiijp?hl=zh-CN</a><br>B站下载助手<a href="https://docs.qq.com/doc/DQ2lhaWRpS0tubVVF" target="_blank" rel="noopener">https://docs.qq.com/doc/DQ2lhaWRpS0tubVVF</a><br>  <a href="https://esu.wiki/Bilibili" target="_blank" rel="noopener">https://esu.wiki/Bilibili</a><br>哔哩哔哩动画第三方客户端<a href="https://github.com/DaweiX/bilibili" target="_blank" rel="noopener">https://github.com/DaweiX/bilibili</a><br>第三方哔哩哔哩API提供平台<a href="https://9bl.bakayun.cn/" target="_blank" rel="noopener">https://9bl.bakayun.cn/</a><br>一个可以观看斗鱼/虎牙/熊猫/B站/全民等直播平台的第三方客户端<a href="https://www.moerats.com/archives/531/" target="_blank" rel="noopener">https://www.moerats.com/archives/531/</a><br>B 站自动领瓜子、直播挂机脚本 <a href="https://github.com/metowolf/BilibiliHelper" target="_blank" rel="noopener">https://github.com/metowolf/BilibiliHelper</a><br><a href="https://docs.rsshub.app/anime.html#_005-tv" target="_blank" rel="noopener">https://docs.rsshub.app/anime.html#_005-tv</a><br>扒视频、视频合并、视频转码<br> <a href="https://media.weibo.cn/article?object_id=1022%3A2309404296500149639994&amp;extparam=lmid--4296500152985262&amp;luicode=10000011&amp;lfid=1076033928669301&amp;id=2309404296500149639994&amp;sudaref=www.google.com.hk&amp;display=0&amp;retcode=6102" target="_blank" rel="noopener">https://media.weibo.cn/article?object_id=1022%3A2309404296500149639994&amp;extparam=lmid–4296500152985262&amp;luicode=10000011&amp;lfid=1076033928669301&amp;id=2309404296500149639994&amp;sudaref=www.google.com.hk&amp;display=0&amp;retcode=6102</a><br>  如何在YouTube和bilibili(哔哩哔哩B站)上评论图片<br><a href="https://sphard.com/%E5%A6%82%E4%BD%95%E5%9C%A8YouTube%E5%92%8Cbilibili(%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9B%E7%AB%99)%E4%B8%8A%E8%AF%84%E8%AE%BA%E5%9B%BE%E7%89%87%EF%BC%8C%E5%9C%A8%E8%AF%84%E8%AE%BA%E9%87%8C%E6%8F%92%E5%85%A5%E5%9B%BE%E7%89%87.html" target="_blank" rel="noopener">https://sphard.com/%E5%A6%82%E4%BD%95%E5%9C%A8YouTube%E5%92%8Cbilibili(%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9B%E7%AB%99)%E4%B8%8A%E8%AF%84%E8%AE%BA%E5%9B%BE%E7%89%87%EF%BC%8C%E5%9C%A8%E8%AF%84%E8%AE%BA%E9%87%8C%E6%8F%92%E5%85%A5%E5%9B%BE%E7%89%87.html</a><br>B站视频下载   <a href="https://github.com/evgo2017/bilibili_video_download" target="_blank" rel="noopener">https://github.com/evgo2017/bilibili_video_download</a><br>B 站排行<a href="https://netobs.top/" target="_blank" rel="noopener">https://netobs.top/</a><br>B 站用户数据收集 <a href="https://github.com/cwjokaka/bilibili_member_crawler" target="_blank" rel="noopener">https://github.com/cwjokaka/bilibili_member_crawler</a><br>B站最近推出了一个在线视频编辑工具，试了下，意外好用！最高可输出1080P！但目前仅支持Chrome浏览器。<a href="https://bilibili.clipchamp.com/editor" target="_blank" rel="noopener">https://bilibili.clipchamp.com/editor</a><br>b 站的倍速油猴脚本，可自定义播放速度，刷新不丢失 <a href="https://greasyfork.org/en/scripts/388225" target="_blank" rel="noopener">https://greasyfork.org/en/scripts/388225</a><br>用python爬虫追踪知乎/B站大V排行 <a href="https://mp.weixin.qq.com/s/unxNMlG-TNMRbG2W1ws6sw" target="_blank" rel="noopener">https://mp.weixin.qq.com/s/unxNMlG-TNMRbG2W1ws6sw</a><br><a href="https://greasyfork.org/en/scripts/381176" target="_blank" rel="noopener">youtube播放视频倍速自定义</a><br>b 站视频封面获取 <a href="https://github.com/jsososo/bilibili-hand-in-the-cover" target="_blank" rel="noopener">https://github.com/jsososo/bilibili-hand-in-the-cover</a><br>自动搬运B博动态至微博的机器人<a href="https://github.com/smilecc/weibo-porter" target="_blank" rel="noopener">https://github.com/smilecc/weibo-porter</a></p>
<p>推荐阅读:</p>
<p><a href="https://mp.weixin.qq.com/s/Xz1m-mqtCcBF_4hmGCpkUQ" target="_blank" rel="noopener">如何发一条空白的朋友圈</a></p>
<p><a href="https://mp.weixin.qq.com/s/1omFkK5PPyeJEzUTagj9qg" target="_blank" rel="noopener">免费在线听周杰伦歌曲</a></p>
<p><a href="https://mp.weixin.qq.com/s/l2bQJk1qjb6IzroODBpoOg" target="_blank" rel="noopener">想看的公众号文章被删了怎么办？</a></p>
<p><a href="https://mp.weixin.qq.com/s/k5lBwiDzGgSU3fh2v2Rw9A" target="_blank" rel="noopener">如何在豆瓣租房小组快速找到满意的房子</a></p>
<p><a href="https://mp.weixin.qq.com/s/qNfuWjH54WHJtx4sEE5xwA" target="_blank" rel="noopener">10%+10% 不等于 0.2？</a></p>
<p><a href="https://mp.weixin.qq.com/s/hbzmqep2JMICL6WogE9TCQ" target="_blank" rel="noopener">有意思的举牌告白小人</a></p>
<h3 id="公众号：苏生不惑"><a href="#公众号：苏生不惑" class="headerlink" title="公众号：苏生不惑"></a>公众号：苏生不惑</h3><p> <img src="https://upload-images.jianshu.io/upload_images/17817191-6e0079f95d4c0338.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="扫描二维码关注"></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/25/"><i class="fa fa-angle-left"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/25/">25</a><span class="page-number current">26</span><a class="page-number" href="/page/27/">27</a><span class="space">&hellip;</span><a class="page-number" href="/page/39/">39</a><a class="extend next" rel="next" href="/page/27/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">苏生不惑</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">382</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">37</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/sushengbuhuo" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mysusheng@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/mysusheng" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i>Google</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://v2ex.com/" title="v2ex" target="_blank">v2ex</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.fanhaobai.com/" title="fanhaobai" target="_blank">fanhaobai</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="yuanxuxu" target="_blank">yuanxuxu</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.snail-c.cn/article" title="snail-c" target="_blank">snail-c</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://showcj.com/archives" title="showcj" target="_blank">showcj</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://vultr.aicnm.com/%E6%9C%80%E6%96%B0Vultr%E6%B3%A8%E5%86%8C%E5%8F%8AVPS%E8%B4%AD%E4%B9%B0%E5%9B%BE%E6%96%87%E6%95%99%E7%A8%8B/" title="vultr" target="_blank">vultr</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lucissfer.com/" title="lucissfer" target="_blank">lucissfer</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/fdipzone/article/details/79352685" title="傲雪星枫" target="_blank">傲雪星枫</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yoby123.cn/index.php/category/default/" title="小白的分享" target="_blank">小白的分享</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/p/5819995.html" title="PHP攻城狮" target="_blank">PHP攻城狮</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.jiaojie.site/" title="php" target="_blank">php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://sphard.com/archives/" title="sphard" target="_blank">sphard</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="LNMP技术栈笔记" target="_blank">LNMP技术栈笔记</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.coding10.com/" title="学习 Laravel" target="_blank">学习 Laravel</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://shuwoom.com/?page_id=929" title="区块链学习指南" target="_blank">区块链学习指南</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://greenlightt.github.io/archives/" title="greenlightt" target="_blank">greenlightt</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.0php.net/archives/" title="0php" target="_blank">0php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.fordba.com/category/mysql" title="mysql" target="_blank">mysql</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.itcodemonkey.com/" title="程序员" target="_blank">程序员</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yanshuo.me/r/v2ex" title="言说" target="_blank">言说</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.timiguo.com/archive.html" title="提米果的博客" target="_blank">提米果的博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://phpartisan.cn/news/112.html" title="phpartisan" target="_blank">phpartisan</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/" title="飞鸿影" target="_blank">飞鸿影</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.54php.cn/" title="54php" target="_blank">54php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lazyman.vip/" title="营销" target="_blank">营销</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.njphper.com/archives/" title="做人呢最重要的就是开心" target="_blank">做人呢最重要的就是开心</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.h57.pw/" title="php 初心者" target="_blank">php 初心者</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">苏生不惑</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>



<div>
<span id="showDays"></span>

</div>

<span id="busuanzi_container_site_pv">
   总访问量:<span id="busuanzi_value_site_pv"></span>次
</span>



  <span class="post-meta-divider">|</span>



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共773.8k字</span>
</div>
<script>
var birthDay = new Date("11/20/2018");
var now = new Date();
var duration = now.getTime() - birthDay.getTime();
var total= Math.floor(duration / (1000 * 60 * 60 * 24));
document.getElementById("showDays").innerHTML = "本站已运行 "+total+" 天";
</script>

        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  











<script type="text/javascript">
    (function() {
        // 匿名函数，防止污染全局变量
        var utterances = document.createElement('script');
        utterances.type = 'text/javascript';
        utterances.async = true;
        utterances.setAttribute('issue-term','0')
        utterances.setAttribute('theme','')
        utterances.setAttribute('repo','sushengbuhuo/laravel_ioc_demo')
        utterances.crossorigin = 'anonymous';
        utterances.src = 'https://utteranc.es/client.js';
        // content 是要插入评论的地方
        document.getElementById('gitment-container').appendChild(utterances);
    })();
</script>


  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

  
<script>
  ((window.gitter = {}).chat = {}).options = {
    //room替换成自己的聊天室名称即可，room的名称规则是：username/roomname
    room: 'sushengbuhuo-chat/mychat'
  };
</script>
<script src="https://sidecar.gitter.im/dist/sidecar.v1.js" async defer></script>

</body>
</html>
